core: fix error reporting of sqlcipher export errors (#1029)

This commit is contained in:
Evgeny Poberezkin
2022-09-07 17:20:47 +01:00
committed by GitHub
parent 766009269e
commit 3f5ca84c84
3 changed files with 16 additions and 8 deletions
+9 -8
View File
@@ -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
+5
View File
@@ -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