Skip to content

Error modules cleanup #7408

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

Open
wants to merge 2 commits into
base: master
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

# 12.0.0-alpha.13 (Unreleased)

#### :boom: Breaking Change

- Rename `JsError` to `JsExn` and error modules cleanup. https://github.com/rescript-lang/rescript/pull/7408

#### :rocket: New Feature

- Add shift (`<<`, `>>`, `>>>`) operators for `int` and `bigint`. https://github.com/rescript-lang/rescript/pull/7183
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type t<+'a> = Js.Promise.t<'a>

exception JsError(Js.Exn.t)
exception JsExn(Js.Exn.t)
external unsafeToJsExn: exn => Js.Exn.t = "%identity"

@new
Expand Down
2 changes: 1 addition & 1 deletion analysis/reanalyze/src/Exn.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let endOfFile = "End_of_file"
let exit = "exit"
let failure = "Failure"
let invalidArgument = "Invalid_argument"
let jsExnError = "Exn.Error"
let jsExn = "JsExn"
let matchFailure = "Match_failure"
let notFound = "Not_found"
let sysError = "Sys_error"
Expand Down
2 changes: 1 addition & 1 deletion analysis/reanalyze/src/Exn.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ val failure : t
val fromLid : Longident.t -> t
val fromString : string -> t
val invalidArgument : t
val jsExnError : t
val jsExn : t
val matchFailure : t
val notFound : t
val sysError : t
Expand Down
42 changes: 21 additions & 21 deletions analysis/reanalyze/src/ExnLib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
in
let stdlib =
[
("panic", [jsExnError]);
("assertEqual", [jsExnError]);
("panic", [jsExn]);
("assertEqual", [jsExn]);
("invalid_arg", [invalidArgument]);
("failwith", [failure]);
("/", [divisionByZero]);
Expand All @@ -50,38 +50,38 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
("float_of_string", [failure]);
]
in
let stdlibBigInt = [("fromStringExn", [jsExnError])] in
let stdlibBigInt = [("fromStringExn", [jsExn])] in
let stdlibBool = [("fromStringExn", [invalidArgument])] in
let stdlibError = [("raise", [jsExnError])] in
let stdlibError = [("raise", [jsExn])] in
let stdlibExn =
[
("raiseError", [jsExnError]);
("raiseEvalError", [jsExnError]);
("raiseRangeError", [jsExnError]);
("raiseReferenceError", [jsExnError]);
("raiseSyntaxError", [jsExnError]);
("raiseTypeError", [jsExnError]);
("raiseUriError", [jsExnError]);
("raiseError", [jsExn]);
("raiseEvalError", [jsExn]);
("raiseRangeError", [jsExn]);
("raiseReferenceError", [jsExn]);
("raiseSyntaxError", [jsExn]);
("raiseTypeError", [jsExn]);
("raiseUriError", [jsExn]);
]
in
let stdlibJson =
[
("parseExn", [jsExnError]);
("parseExnWithReviver", [jsExnError]);
("stringifyAny", [jsExnError]);
("stringifyAnyWithIndent", [jsExnError]);
("stringifyAnyWithReplacer", [jsExnError]);
("stringifyAnyWithReplacerAndIndent", [jsExnError]);
("stringifyAnyWithFilter", [jsExnError]);
("stringifyAnyWithFilterAndIndent", [jsExnError]);
("parseExn", [jsExn]);
("parseExnWithReviver", [jsExn]);
("stringifyAny", [jsExn]);
("stringifyAnyWithIndent", [jsExn]);
("stringifyAnyWithReplacer", [jsExn]);
("stringifyAnyWithReplacerAndIndent", [jsExn]);
("stringifyAnyWithFilter", [jsExn]);
("stringifyAnyWithFilterAndIndent", [jsExn]);
]
in
let stdlibList =
[("headExn", [notFound]); ("tailExn", [notFound]); ("getExn", [notFound])]
in
let stdlibNull = [("getExn", [invalidArgument])] in
let stdlibNullable = [("getExn", [invalidArgument])] in
let stdlibOption = [("getExn", [jsExnError])] in
let stdlibOption = [("getExn", [jsExn])] in
let stdlibResult = [("getExn", [notFound])] in
let yojsonBasic = [("from_string", [yojsonJsonError])] in
let yojsonBasicUtil =
Expand Down Expand Up @@ -144,7 +144,7 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
("Bool", stdlibBool);
("Error", stdlibError);
("Exn", stdlibExn);
("Js.Json", [("parseExn", [jsExnError])]);
("Js.Json", [("parseExn", [jsExn])]);
("JSON", stdlibJson);
("Json_decode", bsJson);
("Json.Decode", bsJson);
Expand Down
4 changes: 2 additions & 2 deletions compiler/core/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ function packError(exn){
}
}
```
So whenever we raise an OCaml exception, we always wrapped it as a JS error.
So whenever we raise an OCaml exception, we always wrapped it as a JS exception.
Now we unpack it, it could be OCaml exception or JS exception, so we did
a runtime dispatch.

Expand All @@ -381,7 +381,7 @@ Some potential optimization
```ocaml
try f x with
Not_found -> ..
JsError e
JsExn e
```

currently it would be transalted as
Expand Down
2 changes: 1 addition & 1 deletion compiler/frontend/ast_exp_extension.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let handle_extension e (self : Bs_ast_mapper.mapper)
in

Exp.apply ~loc
(Exp.ident ~loc {txt = Longident.parse "Js.Exn.raiseError"; loc})
(Exp.ident ~loc {txt = Longident.parse "JsError.throwWithMessage"; loc})
[
( Nolabel,
Exp.constant ~loc
Expand Down
8 changes: 4 additions & 4 deletions compiler/ml/predef.ml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ and ident_error = ident_create_predef_exn "Error"
and ident_dict_magic_field_name =
ident_create Dict_type_helpers.dict_magic_field_name

and ident_js_error = ident_create_predef_exn "JsError"
and ident_js_exn = ident_create_predef_exn "JsExn"

and ident_not_found = ident_create_predef_exn "Not_found"

Expand All @@ -176,7 +176,7 @@ let all_predef_exns =
ident_match_failure;
ident_invalid_argument;
ident_failure;
ident_js_error;
ident_js_exn;
ident_not_found;
ident_end_of_file;
ident_division_by_zero;
Expand Down Expand Up @@ -412,7 +412,7 @@ let common_initial_env add_type add_extension empty_env =
|> add_exception ident_end_of_file []
|> add_exception ident_not_found []
|> add_exception ident_failure [type_string]
|> add_exception ident_js_error [type_unknown]
|> add_exception ident_js_exn [type_unknown]
|> add_exception ident_invalid_argument [type_string]
|> add_exception ident_match_failure
[newgenty (Ttuple [type_string; type_int; type_int])]
Expand All @@ -433,7 +433,7 @@ let builtin_values =
ident_match_failure;
ident_invalid_argument;
ident_failure;
ident_js_error;
ident_js_exn;
ident_not_found;
ident_end_of_file;
ident_division_by_zero;
Expand Down
4 changes: 2 additions & 2 deletions lib/es6/Primitive_exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function internalToException(e) {
return e;
} else {
return {
RE_EXN_ID: "JsError",
RE_EXN_ID: "JsExn",
_1: e
};
}
Expand All @@ -33,7 +33,7 @@ function create(str) {
return str;
}

let $$Error = "JsError";
let $$Error = "JsExn";

export {
$$Error,
Expand Down
12 changes: 9 additions & 3 deletions lib/es6/Stdlib.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


import * as Stdlib_Error from "./Stdlib_Error.js";
import * as Stdlib_Global from "./Stdlib_Global.js";
import * as Stdlib_JsError from "./Stdlib_JsError.js";
import * as Primitive_object from "./Primitive_object.js";

function assertEqual(a, b) {
Expand All @@ -12,7 +12,7 @@ function assertEqual(a, b) {
RE_EXN_ID: "Assert_failure",
_1: [
"Stdlib.res",
120,
122,
4
],
Error: new Error()
Expand Down Expand Up @@ -47,6 +47,10 @@ let Int;

let $$Intl;

let JsError;

let JsExn;

let $$JSON;

let Lazy;
Expand Down Expand Up @@ -117,7 +121,7 @@ let $$BigInt64Array;

let $$BigUint64Array;

let panic = Stdlib_Error.panic;
let panic = Stdlib_JsError.panic;

export {
TimeoutId,
Expand All @@ -134,6 +138,8 @@ export {
Float,
Int,
$$Intl,
JsError,
JsExn,
$$JSON,
Lazy,
List,
Expand Down
2 changes: 1 addition & 1 deletion lib/es6/Stdlib_Exn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as Primitive_option from "./Primitive_option.js";

let $$Error = "JsError";
let $$Error = "JsExn";

function asJsExn(exn) {
if (exn.RE_EXN_ID === $$Error) {
Expand Down
70 changes: 70 additions & 0 deletions lib/es6/Stdlib_JsError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@



function throwWithMessage(str) {
throw new Error(str);
}

function throwWithMessage$1(s) {
throw new EvalError(s);
}

let $$EvalError$1 = {
throwWithMessage: throwWithMessage$1
};

function throwWithMessage$2(s) {
throw new RangeError(s);
}

let $$RangeError$1 = {
throwWithMessage: throwWithMessage$2
};

function throwWithMessage$3(s) {
throw new ReferenceError(s);
}

let $$ReferenceError$1 = {
throwWithMessage: throwWithMessage$3
};

function throwWithMessage$4(s) {
throw new SyntaxError(s);
}

let $$SyntaxError$1 = {
throwWithMessage: throwWithMessage$4
};

function throwWithMessage$5(s) {
throw new TypeError(s);
}

let $$TypeError$1 = {
throwWithMessage: throwWithMessage$5
};

function throwWithMessage$6(s) {
throw new URIError(s);
}

let $$URIError$1 = {
throwWithMessage: throwWithMessage$6
};

function panic(msg) {
throw new Error("Panic! " + msg);
}

export {
$$EvalError$1 as $$EvalError,
$$RangeError$1 as $$RangeError,
$$ReferenceError$1 as $$ReferenceError,
$$SyntaxError$1 as $$SyntaxError,
$$TypeError$1 as $$TypeError,
$$URIError$1 as $$URIError,
throwWithMessage,
panic,
}
/* No side effect */
29 changes: 29 additions & 0 deletions lib/es6/Stdlib_JsExn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


import * as Primitive_option from "./Primitive_option.js";

function fromException(exn) {
if (exn.RE_EXN_ID === "JsExn") {
return Primitive_option.some(exn._1);
}

}

let getOrUndefined = (fieldName => t => (t && typeof t[fieldName] === "string" ? t[fieldName] : undefined));

let stack = getOrUndefined("stack");

let message = getOrUndefined("message");

let name = getOrUndefined("name");

let fileName = getOrUndefined("fileName");

export {
fromException,
stack,
message,
name,
fileName,
}
/* stack Not a pure module */
4 changes: 2 additions & 2 deletions lib/js/Primitive_exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function internalToException(e) {
return e;
} else {
return {
RE_EXN_ID: "JsError",
RE_EXN_ID: "JsExn",
_1: e
};
}
Expand All @@ -33,7 +33,7 @@ function create(str) {
return str;
}

let $$Error = "JsError";
let $$Error = "JsExn";

exports.$$Error = $$Error;
exports.create = create;
Expand Down
Loading