rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Throw Js Erros constructor `EvalError`, `RangeError`, etc

Open aspeddro opened this issue 1 year ago • 6 comments

After #6611 was merged it is no longer possible to throw errors like EvalError. The throw statement now is thow new Error(exn).

Example: The Js.Exn.raiseEvalError function:

https://github.com/rescript-lang/rescript-compiler/blob/0b1443f967808a41c3ad309002eca964f0b6465f/jscomp/others/js_exn.res#L48

compile to:

https://github.com/rescript-lang/rescript-compiler/blob/0b1443f967808a41c3ad309002eca964f0b6465f/lib/es6/js_exn.js#L10-L14

This PR add a function throw:

let throw: exn => 'a = %raw("function (exn) {
  throw exn;
}")

Side question

Here a question arises whether we should support EvalError and others, or encourage ReScript exceptions. Recently documented the exceptions builtin (https://github.com/rescript-association/rescript-lang.org/pull/880)

My idea is to use ReScript exceptions and not bindings to Js Error (i.e @new external make: string => t = "Error"). We will still catch Js Errors

Related #6929

aspeddro avatar Aug 04 '24 00:08 aspeddro

This change will break my libs and applications

DZakh avatar Aug 04 '24 06:08 DZakh

Your libs use Js.Exn.{raiseEvalError,raiseRangeError} functions?

aspeddro avatar Aug 04 '24 15:08 aspeddro

Sorry, I've taken a look at your PR, and it indeed improves things compared to the current v12 alpha release. But it doesn't solve the root of the problem, and for example, the Js.Exn.raiseError still have a regressed behaviour compared to v11.

I'm working on the PR https://github.com/rescript-lang/rescript-compiler/pull/6933, which should automatically fix the issue you're trying to solve in the PR. This is why I think it's not needed.

DZakh avatar Aug 04 '24 18:08 DZakh

What is the regression compared to v11?

aspeddro avatar Aug 05 '24 00:08 aspeddro

What is the regression compared to v11?

Previously it would throw a new Error with the message provided via the arguments. Now it throws new Error("Failure", ... where message is hidden in the cause.

The main usecase for the Js.Exn.raiseError was to throw an error with a readable error message, which would be displayed nicely in logs, but now it's hidden in the cause field

DZakh avatar Aug 05 '24 08:08 DZakh

And if we are talking about the issue https://github.com/rescript-lang/rescript-compiler/issues/6929 Your PR will fix the case with Js.Exn.raiseError, but for the following code it still won't work: https://rescript-lang.org/try?version=v12.0.0-alpha.1&code=C4JwngBA3gUBEFIQEMDuAKABu9BKAvAHxTAAWIA9qhAK4B2AJgKYBmAlnUwwL656a4Y3CAGNkwEaWgwAPhCYAPOhCIQUbAM5N0iuoO4wgA

DZakh avatar Aug 05 '24 08:08 DZakh

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Aug 01 '25 02:08 github-actions[bot]