realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
878 stars 52 forks source link

RLM_ERR_UNSUPPORTED_FILE_FORMAT_VERSION should throw a specific exception, not just IllegalStateException #1761

Open sipersso opened 1 month ago

sipersso commented 1 month ago

Problem

I have an app that is using local realms. It does happen that users take a break from the app and then come back. When dropping support for pre Realm 5.0.0 files, those users are out of luck. I know you can't bring backwards compatibility back, but right now it is not possible to detect that this error has happened on the Kotlin client whereas on iOS realm throws a specific error (Realm.Error.unsupportedFileFormatVersion) that at least allows developers to catch the error and provide a way for the user to reach out to support (or send the file to a server to convert it as suggested by @nirinchev in another github issue).

Solution

Right now this is all I can do is to check if we get an IllegalStateException then inspect the message to see if it is an unsupported file format version error. This is NOT something that should have to go into production code IMHO.

try {
     val realm = Realm.open(configuration)
} catch (error:Exception){
    if(error is IllegalStateException && error.message?.contains("RLM_ERR_UNSUPPORTED_FILE_FORMAT_VERSION") == true){
        //Show dialog to reach out to support or use server function to upgrade database
    }
}

Does the current API let me detect this in any other way? Is it safe to use this hack? Or are my returning customers screwed?

Alternatives

No response

How important is this improvement for you?

Dealbreaker

Feature would mainly be used with

Local Database only

sync-by-unito[bot] commented 1 month ago

➤ PM Bot commented:

Jira ticket: RKOTLIN-1090

kneth commented 1 month ago

Currently there is not elegant way to detect it. We could expose this Realm Core function.

nirinchev commented 1 month ago

Why can't we detect it? As far as I can tell, we have an error code for it: https://github.com/realm/realm-core/blob/3648014ffdcaf6f570de78e1cb4572300c217975/src/realm/error_codes.h#L51

So it should be a matter of switching on the error code and throwing a more concrete exception.

sipersso commented 1 month ago

Why can't we detect it? As far as I can tell, we have an error code for it: https://github.com/realm/realm-core/blob/3648014ffdcaf6f570de78e1cb4572300c217975/src/realm/error_codes.h#L51

So it should be a matter of switching on the error code and throwing a more concrete exception.

This would be the way to go IMHO. You can still use IllegalStateException if you just create a subclass that represents this specific error.