2

Hej, I have this piece of code

BsFirebase.Auth.signInWithEmailAndPassword(
  Firebase.auth,
  ~email=self.state.email,
  ~password=self.state.password,
)
|> Js.Promise.then_(_user => {
  // ...
})
|> Js.Promise.catch((error) => {
  // ...
})
|> ignore;

In the catch, the error field includes a field code that I can use to have more details about the error.

I'm trying to write a wrapper function to cast the error to a "custom" type but I have no idea how to do it.

So far I have (thanks Jared)

type firebaseError = {"code": int, "message": string}; /* etc. */
external unsafeCeorceToFirebaseError: Js.Promise.error => firebaseError = "%identity";

How can I add extra check to be sure the code property exists in the error ?

glennsl
  • 28,186
  • 12
  • 57
  • 75
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77

1 Answers1

1

One way would be to treat the data structure as JSON, if that's appropriate, and use a JSON decoding library like bs-json since those are designed to deal with unknown or unreliable data structures.

Alternatively, you can type each field as a Js.Nullable.t and test them individually upon use.

Although if you can avoid using promise errors for error handling that would be much preferred, as it's not a type safe way of doing it. Use the result type instead and treat promise errors like exceptions.

glennsl
  • 28,186
  • 12
  • 57
  • 75