core: report passphrase error separately from others (#1027)

This commit is contained in:
Evgeny Poberezkin
2022-09-06 23:14:58 +01:00
committed by GitHub
parent 7072dd4f7e
commit de2d169fbc
2 changed files with 21 additions and 6 deletions

View File

@@ -124,7 +124,15 @@ sqlCipherExport DBEncryptionConfig {currentKey = DBEncryptionKey key, newKey = D
where
withDB a err =
liftIO (bracket (SQL.open $ T.pack f) SQL.close a)
`catch` \(e :: SomeException) -> liftIO (putStrLn $ "Database error: " <> show e) >> throwDBError (err $ show e)
`catch` (\(e :: SQL.SQLError) -> log' e >> checkSQLError e)
`catch` (\(e :: SomeException) -> log' e >> throwSQLError e)
where
log' e = liftIO . putStrLn $ "Database error: " <> show e
checkSQLError e = case SQL.sqlError e of
SQL.ErrorNotADatabase -> throwDBError $ err SQLiteErrorNotADatabase
_ -> throwSQLError e
throwSQLError :: Show e => e -> m ()
throwSQLError = throwDBError . err . SQLiteError . show
exportSQL =
T.unlines $
keySQL key

View File

@@ -393,7 +393,7 @@ data ChatError
= ChatError {errorType :: ChatErrorType}
| ChatErrorAgent {agentError :: AgentErrorType}
| ChatErrorStore {storeError :: StoreError}
| ChatErrorDatabase {database :: DatabaseError}
| ChatErrorDatabase {databaseError :: DatabaseError}
deriving (Show, Exception, Generic)
instance ToJSON ChatError where
@@ -455,13 +455,20 @@ data DatabaseError
= DBErrorEncrypted
| DBErrorPlaintext
| DBErrorNoFile {dbFile :: String}
| DBErrorExport {databaseError :: String}
| DBErrorOpen {databaseError :: String}
| DBErrorExport {sqliteError :: SQLiteError}
| DBErrorOpen {sqliteError :: SQLiteError}
deriving (Show, Exception, Generic)
instance ToJSON DatabaseError where
toJSON = J.genericToJSON . sumTypeJSON $ dropPrefix "DBE"
toEncoding = J.genericToEncoding . sumTypeJSON $ dropPrefix "DBE"
toJSON = J.genericToJSON . sumTypeJSON $ dropPrefix "DB"
toEncoding = J.genericToEncoding . sumTypeJSON $ dropPrefix "DB"
data SQLiteError = SQLiteErrorNotADatabase | SQLiteError String
deriving (Show, Exception, Generic)
instance ToJSON SQLiteError where
toJSON = J.genericToJSON . sumTypeJSON $ dropPrefix "SQLite"
toEncoding = J.genericToEncoding . sumTypeJSON $ dropPrefix "SQLite"
throwDBError :: ChatMonad m => DatabaseError -> m ()
throwDBError = throwError . ChatErrorDatabase