[MongoDB Realm] Get Authentication error code on iOS (Swift)

I’d like to get the error code when a login attempt fails, to display the appropriate message to the user. In the doc, the only information about errors is the localizedDescription property, which is in fact, not localized.

How can I get the error codes, or some information to know what error it is? I could always check the localizedDescription, but this is not very stable since the MongoDB team might change the error message at some point.

@Jean-Baptiste_Beau the error returned from an API call will contain the domain and code. You’ll need to cast the Error to NSError with (error as! NSError).code

When doing this, and testing logging in with wrong credentials to get the “invalid user/password” error, the code is -1, is that normal? It doesn’t look like a normal error code

@Lee_Maguire is there a list of possible error codes related to authentication somewhere? I can’t find it in the doc, and this makes me wonder if I can safely rely on the error codes I see when testing or if those might be changed in the future.

Hi @Jean-Baptiste_Beau we don’t have error codes in our documentation, but here is the enum that supplies the error codes to the SDK’s https://github.com/realm/realm-core/blob/fd78e200dffe24a101ecdcacd691ace9607f63f5/src/realm/object-store/sync/generic_network_transport.hpp#L43

Thank you for the link! There seem to be no error code for invalid login credentials, it would be nice to have one to display an appropriate message to the user, as this may be one of the most common errors — and maybe the most important one to display to the user.

Currently the error code for “invalid email/password” is -1 (unknown).

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.