How to programatically get error code?

Hi guys Igot this code to login, the problem is that there is no way to know the reason why failed, I need to show info to the UI or know what type of issue I’m facing. Doc doesn’t say anything about errors.

try {
  const user = await realmApp.logIn(credentials);
  console.log('signed in', user);
} catch (e) {
  console.log(e);
}

It print out:

Error: Request failed (POST https://stitch.mongodb.com/api/client/v2.0/app/xxxx/auth/providers/local-userpass/login): invalid username/password (status 401)
    at Function.fromRequestAndResponse (bundle.dom.es.js:2696)
    at async Fetcher.fetch (bundle.dom.es.js:2864)
    at async Fetcher.fetchJSON (bundle.dom.es.js:2881)
    at async Authenticator.authenticate (bundle.dom.es.js:2592)
    at async App.logIn (bundle.dom.es.js:3073)
    at async handleSignIn (SignIn.js:37)

Programmatically I’m expecting an object with error_code Luke I see on the network http payload but I’m not able to retrieve it on realm-web and act to different situations like wrong password/username or email confirmation pending for example.

This is a cross post to a StackOverflow question

It’s a good idea to post questions in one place to focus possible answers.

Yeah. I tried other community because the lack of support in stackoverflow. I don’t think it’s bad.

Not bad at all! If someone has as answer though, better to focus the attention on that answer instead of searching around for it.

@Juan_Jose_N_A This could definitely use improvement. In the web tutorial we parse the info out using a regex but we realize this shouldn’t be necessary. Instead we could throw a custom RealmAuthError that includes the error code and message as separate properties. We have added this to backlog and hope to get to it in the next quarter

I just spoke with @Ian_Ward offline and we actually do support this already in Realm Web.

I’ve created a small CodeSandbox demonstrating how to get the individual properties from the error object:

@kraenhansen

Is MongoDBRealmError the ‘next generation’ of RealmError where you could compare it with the Enums to handle the error?

See my ‘attempt’ at an answer to the same question on SO - if RealmError is gone then I will delete that as it’s no longer applicable.

I tested and yes it works already on web. Amazing. But it need to be documented now it is just a secret.

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