diff --git a/src/Simplex/Chat/Archive.hs b/src/Simplex/Chat/Archive.hs index 86c2176f6..f4bcc5ca4 100644 --- a/src/Simplex/Chat/Archive.hs +++ b/src/Simplex/Chat/Archive.hs @@ -15,6 +15,7 @@ where import qualified Codec.Archive.Zip as Z import Control.Monad.Except import Control.Monad.Reader +import Data.Functor (($>)) import qualified Data.Text as T import qualified Database.SQLite3 as SQL import Simplex.Chat.Controller @@ -123,16 +124,16 @@ sqlCipherExport DBEncryptionConfig {currentKey = DBEncryptionKey key, newKey = D atomically $ writeTVar dbEnc $ not (null key') where withDB a err = - liftIO (bracket (SQL.open $ T.pack f) SQL.close a) - `catch` (\(e :: SQL.SQLError) -> log' e >> checkSQLError e) - `catch` (\(e :: SomeException) -> log' e >> throwSQLError e) + liftIO (bracket (SQL.open $ T.pack f) SQL.close a $> Nothing) + `catch` checkSQLError + `catch` (\(e :: SomeException) -> sqliteError' e) + >>= mapM_ (throwDBError . err) 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 + SQL.ErrorNotADatabase -> pure $ Just SQLiteErrorNotADatabase + _ -> sqliteError' e + sqliteError' :: Show e => e -> m (Maybe SQLiteError) + sqliteError' = pure . Just . SQLiteError . show exportSQL = T.unlines $ keySQL key diff --git a/src/Simplex/Chat/View.hs b/src/Simplex/Chat/View.hs index 7fa7d0ac3..fbf9878c6 100644 --- a/src/Simplex/Chat/View.hs +++ b/src/Simplex/Chat/View.hs @@ -946,6 +946,8 @@ viewChatError = \case ChatErrorDatabase err -> case err of DBErrorEncrypted -> ["error: chat database is already encrypted"] DBErrorPlaintext -> ["error: chat database is not encrypted"] + DBErrorExport e -> ["error encrypting database: " <> sqliteError' e] + DBErrorOpen e -> ["error opening database after encryption: " <> sqliteError' e] e -> ["chat database error: " <> sShow e] ChatErrorAgent err -> case err of SMP SMP.AUTH -> @@ -958,6 +960,9 @@ viewChatError = \case e -> ["smp agent error: " <> sShow e] where fileNotFound fileId = ["file " <> sShow fileId <> " not found"] + sqliteError' = \case + SQLiteErrorNotADatabase -> "wrong passphrase or invalid database file" + SQLiteError e -> sShow e ttyContact :: ContactName -> StyledString ttyContact = styled $ colored Green diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index 6a6ea4ac7..983258fd4 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -2788,6 +2788,8 @@ testDatabaseEncryption = withTmpFiles $ do testChatWorking alice bob alice ##> "/_stop" alice <## "chat stopped" + alice ##> "/db password wrongkey nextkey" + alice <## "error encrypting database: wrong passphrase or invalid database file" alice ##> "/db password mykey nextkey" alice <## "ok" alice ##> "/_db encryption {\"currentKey\":\"nextkey\",\"newKey\":\"anotherkey\"}"