Skip to main content

Error Handling

DCharVault uses a typed DiaryError enum to communicate failure conditions from the Model layer up through the ViewModel and into the UI. This guide documents every error code and the recommended recovery strategy for each.


The DiaryError Enum​

DiaryError is returned by any Model-layer operation that can fail in a meaningful, recoverable way. It is defined in the core model headers and is the primary mechanism for communicating failures without throwing exceptions.

enum class DiaryError {
// Success
Success,

// Vault / Authentication Errors
VaultAlreadyOpen,
VaultNotOpen,
AuthenticationFailed,
InvalidMAC,
DatabaseNotFound,
DatabaseCorrupt,

// Entry Operation Errors
EntryNotFound,
EncryptionFailed,
DecryptionFailed,
DatabaseWriteFailed,
DatabaseReadFailed,

// Session / Configuration Errors
ConfigWriteFailed,
ConfigReadFailed,

// Memory / System Errors
SecureMemoryError,
UnknownError,
};

Error Code Reference​

Vault & Authentication Errors​

CodeMeaningRecommended Recovery
SuccessOperation completed without errorContinue normally
VaultAlreadyOpenopenDiary was called when a vault was already activeCall lockVault() first, then retry
VaultNotOpenAn operation was attempted on a closed/locked vaultNavigate user to the login screen
AuthenticationFailedThe master password did not produce a valid keyPrompt user to retry; do not log the attempt in plaintext
InvalidMACThe verification MAC block failed authentication — database may be tampered withWarn the user of potential data integrity issues; do not decrypt further
DatabaseNotFoundThe specified .db file path does not existEmit dbNotFound signal; prompt user to select a valid vault file
DatabaseCorruptThe SQLite database file is structurally invalidShow a non-recoverable error; advise the user to restore from backup

Entry Operation Errors​

CodeMeaningRecommended Recovery
EntryNotFoundNo entry exists for the given idRefresh the entry list via DiaryListModel::loadEntries()
EncryptionFailedLibsodium encryption operation returned an errorRetry once; if it persists, it may indicate a SecureMemory allocation failure
DecryptionFailedDecryption or authentication of an entry failedThe entry may be corrupted; surface the error to the user without exposing partial plaintext
DatabaseWriteFailedAn INSERT or UPDATE SQL operation failedCheck available disk space; retry the operation
DatabaseReadFailedA SELECT SQL operation returned an unexpected resultRefresh the entry list; if persistent, may indicate corruption

Session & Configuration Errors​

CodeMeaningRecommended Recovery
ConfigWriteFailedSaving a configuration value (e.g., timeout) to the database failedShow a non-blocking warning to the user; setting will revert to default on next launch
ConfigReadFailedReading a configuration value failedFall back to the default value silently

System Errors​

CodeMeaningRecommended Recovery
SecureMemoryErrorsodium_malloc failed to allocate a guarded memory regionThis is a critical system-level failure; lock the vault immediately and surface an error
UnknownErrorAn unclassified error occurredLog the context and surface a generic error to the user

Handling DiaryError in ViewModels​

ViewModels receive DiaryError values from the Model layer and translate them into Qt signals that the QML UI can react to. Here is the standard pattern:

// In LoginViewModel::authenticate(...)
DiaryError result = m_diaryManager->openDiary(name, path, password);

switch (result) {
case DiaryError::Success:
emit loginSuccess();
break;
case DiaryError::AuthenticationFailed:
case DiaryError::InvalidMAC:
emit loginFailed();
break;
case DiaryError::DatabaseNotFound:
emit dbNotFound();
break;
default:
emit loginFailed(); // Treat unknown vault errors as auth failures
break;
}
// In DiaryViewModel::saveNewEntry(...)
DiaryError result = m_diaryManager->updateEntry(id, title, content);

if (result != DiaryError::Success) {
emit entrySaveFailed(diaryErrorToString(result));
} else {
emit entrySavedSuccessfully(id, title);
}

Security Considerations​

  • Never log plaintext passwords or decrypted content alongside error messages, even in debug builds.
  • InvalidMAC is a security-critical error. It may indicate tampering. Do not partially decrypt or expose any data when this error is returned.
  • DecryptionFailed entries should be surfaced to the user rather than silently skipped — silent failures can mask data corruption.
  • SecureMemoryError is non-recoverable. The vault must be locked immediately to prevent any sensitive data from remaining in unprotected heap memory.