davidmartos96 / sqflite_sqlcipher

SQLite flutter plugin
BSD 2-Clause "Simplified" License
96 stars 41 forks source link

When `openDatabase` fails, there is a fallback with `PRAGMA cipher_migrate` that hides the original exception #86

Closed akvus closed 2 months ago

akvus commented 5 months ago

(Android)

openDatabase can fail for many reasons. The current implementation catches the original exception and tries to open the database again with PRAGMA cipher_migrate;. When this call fails, then we receive an error "SQL logic error: , while compiling: select count(*) from sqlite_master;" which is caused by the migration not working, while that's not the problem. The problem may be an incorrect password. However, we will never know that in Flutter, because the original exception was swallowed.

See https://github.com/davidmartos96/sqflite_sqlcipher/blob/e6dc64dfdb67d427b6c490f971778552ce93aa04/sqflite/android/src/main/java/com/davidmartos96/sqflite_sqlcipher/Database.java#L71C10-L71C10

davidmartos96 commented 5 months ago

Interesting situation. Thanks for bringing it up. I'm not sure if there is a good way to obtain the correct error while keeping the migration logic. Feel free to open a PR if you experiment a possible fix. There is a test suite related to SQLCipher in the example folder. One of the tests opens a v3 database, so it should be easy to add another test case for when the password is incorrect and check for the correct error message.

davidmartos96 commented 2 months ago

@akvus Hello! I've just published a new version of the package that doesn't hide the original exception. Thanks for reporting!

akvus commented 2 months ago

Thank you @davidmartos96!