diff --git a/src/Simplex/Chat/Archive.hs b/src/Simplex/Chat/Archive.hs index 6eb23bcc0d..86c2176f67 100644 --- a/src/Simplex/Chat/Archive.hs +++ b/src/Simplex/Chat/Archive.hs @@ -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 diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index 37527ea065..9d4d1cae4b 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -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