Rescript should throw better exceptions

Currently, some of my exceptions translate to this.

    throw {
          RE_EXN_ID: "Failure",
          _1: message,
          Error: new Error()
        };

But I believe Rescript should create a class and then throw that. Furthermore, the resulting error prints an error along with the stack trace in Firefox instead of showing a stack trace stopping.

2 Likes

Rescript has it’s own exception system that you use when doing exception Foo and raise(Foo). These are meant to be caught within your rescript codebase and pattern matched on in the catch case, and it uses the RE_EXN_ID to make that happen

exception Foo

try raise(Foo) catch {
| Foo => Js.log("Caught foo!")
}

If you’re throwing an error that you don’t expect to handle, or are throwing errors for JS consumers, in Rescript 11 you can use Error.make and Error.raise from rescript/core

Error.make("Foo!")->Error.raise // throw new Error("Foo!")
1 Like

A lot of time I use getExn and I don’t intend to catch that.

That’s the point of OP:

I agree it is a sticking point. Specially as more and more things are fixed and better integrated with JS. I also agree that in future versions it would be great if exceptions got turned to first class JS exceptions and they avoided that idiosyncrasy.

1 Like