Creating bindings for a custom error type

Hello,

I’m trying to create some bindings for the following: https.HttpsError class  |  Cloud Functions for Firebase

I want to create a new object (of HttpsError) and raise it in a function.
So, I’m not quite sure how to describe that.

Next I also want to make this string enum to a type, https namespace  |  Cloud Functions for Firebase
Is that possible in ReScript?

So far I got:

    module HttpsError = {
      type functionsErrorCode =
        | @as("ok") Ok
        | @as("cancelled") Cancelled

      type t

      @module("firebase-functions/v2/https") @new
      external make: (~code: functionsErrorCode, ~message: string=?, ~data: {..}=?) => t = "HttpsError"

      external asException : t => Error.t = "%identity"
    }

// later usage
    if request.data == "error" {
      let ex = HttpsError.make(~code=HttpsError.Cancelled, ~message="Sending error from the server")->HttpsError.asException
      Error.raise(ex)
    }

Is that asException the way to go?

That looks like the way to go to me too!

1 Like

You actually don’t need the asException identity function as your variants don’t have a payload and are thus coercible to strings. But it might be more convenient still.

type functionsErrorCode =
  | @as("ok") Ok
  | @as("cancelled") Cancelled

let error = (Cancelled :> string)->Error.make

To play ball with Firebase, I need to raise an instance of

Which does hold data.

Sorry, I misread your initial post.

Your initial implementation is what I would do as well then.

2 Likes