Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecated Exn module #230

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Next version

- Deprecated `Exn` module. Use the `Error` module instead. https://github.com/rescript-association/rescript-core/pull/230

## 1.5.2

- Remove aliases for runtime modules (`MapperRt`, `Internal`) and `Re`. https://github.com/rescript-association/rescript-core/pull/237
Expand Down
2 changes: 1 addition & 1 deletion src/Core__BigInt.res
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ BigInt.fromStringExn("0o11")
try {
BigInt.fromStringExn("a")
} catch {
| Exn.Error(_error) => 0n
| Error.Error(_error) => 0n
}
```
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Core__Error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ function panic(msg) {
throw new Error("Panic! " + msg);
}

var $$Error$1 = "JsError";

export {
$$Error$1 as $$Error,
$$EvalError ,
$$RangeError ,
$$ReferenceError ,
Expand Down
8 changes: 7 additions & 1 deletion src/Core__Error.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
type t = Js.Exn.t
type t = unknown

@@warning("-38") /* unused extension constructor */
exception Error = JsError

external fromException: exn => option<t> = "?as_js_exn"
external toException: t => exn = "%identity"
Expand All @@ -10,6 +13,9 @@ external toException: t => exn = "%identity"

@new external make: string => t = "Error"

external isCamlExceptionOrOpenVariant: 'a => bool = "?is_extension"
external anyToExnInternal: 'a => exn = "#wrap_exn"

module EvalError = {
@new external make: string => t = "EvalError"
}
Expand Down
33 changes: 32 additions & 1 deletion src/Core__Error.resi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ See [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/
*/

/** Represents a JavaScript exception. */
type t = Js.Exn.t
type t

type exn += private Error(t)

external fromException: exn => option<t> = "?as_js_exn"

Expand Down Expand Up @@ -86,6 +88,35 @@ Console.log(error->Error.name) // Logs "Error" to the console, because this is a
@new
external make: string => t = "Error"

/** internal use only */
external isCamlExceptionOrOpenVariant: 'a => bool = "?is_extension"

/**
`anyToExnInternal(obj)` will take any value `obj` and wrap it
in a `Error.Error` if given value is not an exn already. If
`obj` is an exn, it will return `obj` without any changes.

This function is mostly useful for cases where you want to unify a type of a value
that potentially is either exn, a JS error, or any other JS value really (e.g. for
a value passed to a Promise.catch callback)

**IMPORTANT**: This is an internal API and may be changed / removed any time in the future.

## Examples

```rescript
switch Error.anyToExnInternal("test") {
| Error.Error(v) =>
switch(Error.message(v)) {
| Some(_) => assert(false)
| None => Console.log2("We will land here", v)
}
| _ => assert(false)
}
```
*/
external anyToExnInternal: 'a => exn = "#wrap_exn"

module EvalError: {
/**
Creates a new `EvalError` with the provided `message`.
Expand Down
16 changes: 8 additions & 8 deletions src/Core__JSON.resi
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ try {
let _ = JSON.parseExn("")
// error
} catch {
| Exn.Error(_) => Console.log("error")
| Error.Error(_) => Console.log("error")
}

let reviver = (_, value: JSON.t) =>
Expand All @@ -52,15 +52,15 @@ try {
JSON.parseExn("", ~reviver)->Console.log
// error
} catch {
| Exn.Error(_) => Console.log("error")
| Error.Error(_) => Console.log("error")
}
```

## Exceptions

- Raises a SyntaxError (Exn.t) if the string isn't valid JSON.
- Raises a SyntaxError (Error.t) if the string isn't valid JSON.
*/
@raises(Exn.t)
@raises(Error.t)
@val
external parseExn: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"

Expand Down Expand Up @@ -89,7 +89,7 @@ try {
JSON.parseExnWithReviver("", reviver)->Console.log
// error
} catch {
| Exn.Error(_) => Console.log("error")
| Error.Error(_) => Console.log("error")
}
```

Expand All @@ -98,7 +98,7 @@ try {
- Raises a SyntaxError if the string isn't valid JSON.
*/
@deprecated("Use `parseExn` with optional parameter instead")
@raises(Exn.t)
@raises(Error.t)
@val
external parseExnWithReviver: (string, (string, t) => t) => t = "JSON.parse"

Expand Down Expand Up @@ -350,7 +350,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
- Raises a TypeError if the value contains circular references.
- Raises a TypeError if the value contains `BigInt`s.
*/
@raises(Exn.t)
@raises(Error.t)
@val
external stringifyAny: ('a, ~replacer: replacer=?, ~space: int=?) => option<string> =
"JSON.stringify"
Expand Down Expand Up @@ -391,7 +391,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
- Raises a TypeError if the value contains `BigInt`s.
*/
@deprecated("Use `stringifyAny` with optional parameter instead")
@raises(Exn.t)
@raises(Error.t)
@val
external stringifyAnyWithIndent: ('a, @as(json`null`) _, int) => option<string> = "JSON.stringify"

Expand Down
2 changes: 1 addition & 1 deletion src/Core__Promise.res
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ external _catch: (t<'a>, exn => t<'a>) => t<'a> = "catch"

let catch = (promise: promise<'a>, callback: exn => promise<'a>): promise<'a> => {
_catch(promise, err => {
callback(Js.Exn.anyToExnInternal(err))
callback(Core__Error.anyToExnInternal(err))
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/Core__Promise.resi
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ reject(SomeError("this is an error"))
->catch(e => {
let msg = switch(e) {
| SomeError(msg) => "ReScript error occurred: " ++ msg
| Exn.Error(obj) =>
switch Exn.message(obj) {
| Error.Error(obj) =>
switch Error.message(obj) {
| Some(msg) => "JS exception occurred: " ++ msg
| None => "Some other JS value has been thrown"
}
Expand Down
2 changes: 2 additions & 0 deletions src/RescriptCore.res
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ async function main() {
*/
external import: 'a => promise<'a> = "#import"

@deprecated("Use `Error` module instead")
module Exn = Js.Exn

module Option = Core__Option
module List = Core__List
module Result = Core__Result
Expand Down
4 changes: 2 additions & 2 deletions test/ErrorTests.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Test from "./Test.mjs";
import * as Js_exn from "rescript/lib/es6/js_exn.js";
import * as Core__Error from "../src/Core__Error.mjs";
import * as RescriptCore from "../src/RescriptCore.mjs";
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";

Expand All @@ -12,7 +12,7 @@ function panicTest() {
}
catch (raw_err){
var err = Caml_js_exceptions.internalToOCamlException(raw_err);
if (err.RE_EXN_ID === Js_exn.$$Error) {
if (err.RE_EXN_ID === Core__Error.$$Error) {
caught = err._1.message;
} else {
throw err;
Expand Down
2 changes: 1 addition & 1 deletion test/ErrorTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open RescriptCore

let panicTest = () => {
let caught = try panic("uh oh") catch {
| Exn.Error(err) => Error.message(err)
| Error.Error(err) => Error.message(err)
}

Test.run(__POS_OF__("Should resolve test"), caught, \"==", Some("Panic! uh oh"))
Expand Down
4 changes: 2 additions & 2 deletions test/IntTests.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Test from "./Test.mjs";
import * as Js_exn from "rescript/lib/es6/js_exn.js";
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
import * as Core__Int from "../src/Core__Int.mjs";
import * as Core__Error from "../src/Core__Error.mjs";
import * as PervasivesU from "rescript/lib/es6/pervasivesU.js";
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";

Expand All @@ -16,7 +16,7 @@ function $$catch(f) {
}
catch (raw_err){
var err = Caml_js_exceptions.internalToOCamlException(raw_err);
if (err.RE_EXN_ID === Js_exn.$$Error) {
if (err.RE_EXN_ID === Core__Error.$$Error) {
return err._1;
}
throw err;
Expand Down
2 changes: 1 addition & 1 deletion test/IntTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let catch = f =>
let _ = f()
failwith("no exception raised")
} catch {
| Exn.Error(err) => err
| Error.Error(err) => err
}

Test.run(__POS_OF__("range - positive, increasing"), Int.range(3, 6), eq, [3, 4, 5])
Expand Down
12 changes: 7 additions & 5 deletions test/PromiseTest.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Test from "./Test.mjs";
import * as Js_exn from "rescript/lib/es6/js_exn.js";
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
import * as Core__Error from "../src/Core__Error.mjs";
import * as Core__Promise from "../src/Core__Promise.mjs";
import * as Caml_exceptions from "rescript/lib/es6/caml_exceptions.js";

var TestError = /* @__PURE__ */Caml_exceptions.create("PromiseTest.TestError");

var fail = Js_exn.raiseError;
function fail(msg) {
throw msg;
}

var equal = Caml_obj.equal;

Expand Down Expand Up @@ -161,7 +163,7 @@ function testExternalPromiseThrow() {
return Core__Promise.$$catch(asyncParseFail().then(function (param) {
return Promise.resolve();
}), (function (e) {
var success = e.RE_EXN_ID === Js_exn.$$Error ? Caml_obj.equal(e._1.name, "SyntaxError") : false;
var success = e.RE_EXN_ID === Core__Error.$$Error ? Caml_obj.equal(e._1.name, "SyntaxError") : false;
Test.run([
[
"PromiseTest.res",
Expand Down Expand Up @@ -199,9 +201,9 @@ function testExnThrow() {

function testRaiseErrorThrow() {
return Core__Promise.$$catch(Promise.resolve().then(function () {
return Js_exn.raiseError("Some JS error");
throw new Error("Some JS error");
}), (function (e) {
var isTestErr = e.RE_EXN_ID === Js_exn.$$Error ? Caml_obj.equal(e._1.message, "Some JS error") : false;
var isTestErr = e.RE_EXN_ID === Core__Error.$$Error ? Caml_obj.equal(e._1.message, "Some JS error") : false;
Test.run([
[
"PromiseTest.res",
Expand Down
8 changes: 4 additions & 4 deletions test/PromiseTest.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ open RescriptCore
exception TestError(string)

let fail = msg => {
Exn.raiseError(msg)
Error.raise(msg)
}

let equal = (a, b) => {
Expand Down Expand Up @@ -132,7 +132,7 @@ module Catching = {
->then(_ => resolve()) // Since our asyncParse will fail anyways, we convert to promise<unit> for our catch later
->catch(e => {
let success = switch e {
| Exn.Error(err) => Exn.name(err) == Some("SyntaxError")
| Error.Error(err) => Error.name(err) == Some("SyntaxError")
| _ => false
}

Expand Down Expand Up @@ -166,7 +166,7 @@ module Catching = {
open Promise

let causeErr = () => {
Exn.raiseError("Some JS error")
Error.make("Some JS error")->Error.raise
}

resolve()
Expand All @@ -175,7 +175,7 @@ module Catching = {
})
->catch(e => {
let isTestErr = switch e {
| Exn.Error(err) => Exn.message(err) == Some("Some JS error")
| Error.Error(err) => Error.message(err) == Some("Some JS error")
| _ => false
}
Test.run(__POS_OF__("Should be some JS error"), isTestErr, equal, true)
Expand Down
8 changes: 4 additions & 4 deletions test/intl/IntlTests.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Js_exn from "rescript/lib/es6/js_exn.js";
import * as Caml_option from "rescript/lib/es6/caml_option.js";
import * as Core__Error from "../../src/Core__Error.mjs";
import * as Core__Option from "../../src/Core__Option.mjs";
import * as Intl__LocaleTest from "./Intl__LocaleTest.mjs";
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
Expand Down Expand Up @@ -29,7 +29,7 @@ try {
}
catch (raw_e){
var e = Caml_js_exceptions.internalToOCamlException(raw_e);
if (e.RE_EXN_ID === Js_exn.$$Error) {
if (e.RE_EXN_ID === Core__Error.$$Error) {
console.error(e._1);
} else {
throw e;
Expand All @@ -46,7 +46,7 @@ try {
}
catch (raw_e$1){
var e$1 = Caml_js_exceptions.internalToOCamlException(raw_e$1);
if (e$1.RE_EXN_ID === Js_exn.$$Error) {
if (e$1.RE_EXN_ID === Core__Error.$$Error) {
console.error(e$1._1);
} else {
throw e$1;
Expand All @@ -59,7 +59,7 @@ try {
}
catch (raw_e$2){
var e$2 = Caml_js_exceptions.internalToOCamlException(raw_e$2);
if (e$2.RE_EXN_ID === Js_exn.$$Error) {
if (e$2.RE_EXN_ID === Core__Error.$$Error) {
var e$3 = e$2._1;
var message = Core__Option.map(e$3.message, (function (prim) {
return prim.toLowerCase();
Expand Down
6 changes: 3 additions & 3 deletions test/intl/IntlTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Intl.getCanonicalLocalesManyExn(["EN-US", "Fr"])->Console.log
try {
Intl.getCanonicalLocalesExn("bloop")->Console.log
} catch {
| Exn.Error(e) => Console.error(e)
| Error.Error(e) => Console.error(e)
}

try {
Expand All @@ -29,15 +29,15 @@ try {
Intl.supportedValuesOfExn("timeZone")->Console.log
Intl.supportedValuesOfExn("unit")->Console.log
} catch {
| Exn.Error(e) => Console.error(e)
| Error.Error(e) => Console.error(e)
}

try {
Intl.supportedValuesOfExn("someInvalidKey")->ignore

Console.error("Shouldn't have been hit")
} catch {
| Exn.Error(e) =>
| Error.Error(e) =>
switch Error.message(e)->Option.map(String.toLowerCase) {
| Some("invalid key : someinvalidkey") => Console.log("Caught expected error")
| message => {
Expand Down
Loading