From 8c06a6088000ce8ecb3f99ec9784c31e42947c49 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 25 Feb 2023 13:39:11 +0100 Subject: [PATCH 1/4] feat(error): add panic/Error.panic refactor(error): less magic needed Co-authored-by: Christoph Knittel docs(error): panic fixes Co-authored-by: Christoph Knittel docs(error): ReScript, not OCaml --- src/Core__Error.mjs | 5 +++++ src/Core__Error.res | 2 ++ src/Core__Error.resi | 14 ++++++++++++++ src/RescriptCore.mjs | 4 ++++ src/RescriptCore.res | 2 ++ 5 files changed, 27 insertions(+) diff --git a/src/Core__Error.mjs b/src/Core__Error.mjs index b9e0a7a0..e5334cf8 100644 --- a/src/Core__Error.mjs +++ b/src/Core__Error.mjs @@ -13,6 +13,10 @@ var $$TypeError = {}; var $$URIError = {}; +function panic(msg) { + throw new Error("Panic! " + msg); +} + export { $$EvalError , $$RangeError , @@ -20,5 +24,6 @@ export { $$SyntaxError , $$TypeError , $$URIError , + panic , } /* No side effect */ diff --git a/src/Core__Error.res b/src/Core__Error.res index 69e05994..788e0bdb 100644 --- a/src/Core__Error.res +++ b/src/Core__Error.res @@ -35,3 +35,5 @@ module URIError = { } external raise: t => 'a = "%raise" + +let panic = msg => make(j`Panic! $msg`)->raise diff --git a/src/Core__Error.resi b/src/Core__Error.resi index 563c6fc2..76c12c0c 100644 --- a/src/Core__Error.resi +++ b/src/Core__Error.resi @@ -155,3 +155,17 @@ if 5 > 10 { ``` */ external raise: t => 'a = "%raise" + +/** +Raises a panic exception with the given message. + +A panic exception is a native JavaScript exception that is not intended to be caught and +handled. Compared to a ReScript exception this will give a better stack trace and +debugging experience. + +## Examples +```rescript +Error.panic("Uh oh. This was unexpected!") +``` +*/ +let panic: string => 'a diff --git a/src/RescriptCore.mjs b/src/RescriptCore.mjs index 7706a777..334f6e2d 100644 --- a/src/RescriptCore.mjs +++ b/src/RescriptCore.mjs @@ -1,5 +1,6 @@ // Generated by ReScript, PLEASE EDIT WITH CARE +import * as Core__Error from "./Core__Error.mjs"; var $$Array; @@ -93,6 +94,8 @@ var List; var Result; +var panic = Core__Error.panic; + export { $$Array , Console , @@ -140,5 +143,6 @@ export { $$Option , List , Result , + panic , } /* No side effect */ diff --git a/src/RescriptCore.res b/src/RescriptCore.res index 7cd77db2..d76bcde0 100644 --- a/src/RescriptCore.res +++ b/src/RescriptCore.res @@ -65,3 +65,5 @@ type null<+'a> = Js.null<'a> type undefined<+'a> = Js.undefined<'a> type nullable<+'a> = Js.nullable<'a> + +let panic = Core__Error.panic From cb56e448fb3fd86902780028451b57f05e1e773b Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 25 Feb 2023 13:40:00 +0100 Subject: [PATCH 2/4] test(error): test panic --- package.json | 6 ++---- test/ErrorTests.mjs | 39 +++++++++++++++++++++++++++++++++++++++ test/ErrorTests.res | 11 +++++++++++ test/TestSuite.mjs | 35 +++++++++++++++++++++++++++++++++++ test/TestSuite.res | 2 ++ 5 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 test/ErrorTests.mjs create mode 100644 test/ErrorTests.res create mode 100644 test/TestSuite.mjs create mode 100644 test/TestSuite.res diff --git a/package.json b/package.json index d23f0f91..f4a3fb7b 100644 --- a/package.json +++ b/package.json @@ -5,11 +5,9 @@ "clean": "rescript clean", "build": "rescript", "watch": "rescript build -w", - "test": "node test/PromiseTest.mjs && node test/TempTests.mjs" + "test": "node test/TestSuite.mjs && node test/TempTests.mjs" }, - "keywords": [ - "rescript" - ], + "keywords": ["rescript"], "homepage": "https://github.com/rescript-association/rescript-core", "author": "ReScript Team", "license": "MIT", diff --git a/test/ErrorTests.mjs b/test/ErrorTests.mjs new file mode 100644 index 00000000..a9af7afb --- /dev/null +++ b/test/ErrorTests.mjs @@ -0,0 +1,39 @@ +// 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 RescriptCore from "../src/RescriptCore.mjs"; +import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js"; + +function panicTest(param) { + var caught; + try { + caught = RescriptCore.panic("uh oh"); + } + catch (raw_err){ + var err = Caml_js_exceptions.internalToOCamlException(raw_err); + if (err.RE_EXN_ID === Js_exn.$$Error) { + caught = err._1.message; + } else { + throw err; + } + } + Test.run([ + [ + "ErrorTests.res", + 8, + 22, + 43 + ], + "Should resolve test" + ], caught, (function (prim0, prim1) { + return prim0 === prim1; + }), "Panic! uh oh"); +} + +panicTest(undefined); + +export { + panicTest , +} +/* Not a pure module */ diff --git a/test/ErrorTests.res b/test/ErrorTests.res new file mode 100644 index 00000000..07dcc078 --- /dev/null +++ b/test/ErrorTests.res @@ -0,0 +1,11 @@ +open RescriptCore + +let panicTest = () => { + let caught = try panic("uh oh") catch { + | Exn.Error(err) => Error.message(err) + } + + Test.run(__POS_OF__("Should resolve test"), caught, \"==", Some("Panic! uh oh")) +} + +panicTest() diff --git a/test/TestSuite.mjs b/test/TestSuite.mjs new file mode 100644 index 00000000..bde8356a --- /dev/null +++ b/test/TestSuite.mjs @@ -0,0 +1,35 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as ErrorTests from "./ErrorTests.mjs"; +import * as PromiseTest from "./PromiseTest.mjs"; + +var TestError = PromiseTest.TestError; + +var fail = PromiseTest.fail; + +var equal = PromiseTest.equal; + +var Creation = PromiseTest.Creation; + +var ThenChaining = PromiseTest.ThenChaining; + +var Rejection = PromiseTest.Rejection; + +var Catching = PromiseTest.Catching; + +var Concurrently = PromiseTest.Concurrently; + +var panicTest = ErrorTests.panicTest; + +export { + TestError , + fail , + equal , + Creation , + ThenChaining , + Rejection , + Catching , + Concurrently , + panicTest , +} +/* ErrorTests Not a pure module */ diff --git a/test/TestSuite.res b/test/TestSuite.res new file mode 100644 index 00000000..c2ae6367 --- /dev/null +++ b/test/TestSuite.res @@ -0,0 +1,2 @@ +include PromiseTest +include ErrorTests From 56886f02e3fffe02835f5e17aa331385a3d593ab Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 25 Feb 2023 14:06:23 +0100 Subject: [PATCH 3/4] docs(changelog): update changelog for panic/Error.panic --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc992beb..b8809183 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - Change `Float.parseFloat` signature. Now accepts only string. https://github.com/rescript-association/rescript-core/pull/54 - Add `getExn`, `getUnsafe`, `getWithDefault`, `map`, `mapWithDefault` and `flatMap` to `Nullable`. https://github.com/rescript-association/rescript-core/pull/67 - Add `getExn`, `getUnsafe`, `getWithDefault`, `map`, `mapWithDefault` and `flatMap` to `Null`. https://github.com/rescript-association/rescript-core/pull/73 +- Add `panic`/`Error.panic` and use that where appropriate: https://github.com/rescript-association/rescript-core/pull/72 ### Documentation From 7439a3112c8119872b37a0073e5039bb3007a937 Mon Sep 17 00:00:00 2001 From: Glenn Slotte Date: Wed, 1 Mar 2023 12:41:05 +0100 Subject: [PATCH 4/4] docs(changelog): adjust panic entry after splitting PR Co-authored-by: Christoph Knittel --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8809183..b690761e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ - Change `Float.parseFloat` signature. Now accepts only string. https://github.com/rescript-association/rescript-core/pull/54 - Add `getExn`, `getUnsafe`, `getWithDefault`, `map`, `mapWithDefault` and `flatMap` to `Nullable`. https://github.com/rescript-association/rescript-core/pull/67 - Add `getExn`, `getUnsafe`, `getWithDefault`, `map`, `mapWithDefault` and `flatMap` to `Null`. https://github.com/rescript-association/rescript-core/pull/73 -- Add `panic`/`Error.panic` and use that where appropriate: https://github.com/rescript-association/rescript-core/pull/72 +- Add `panic`/`Error.panic`. https://github.com/rescript-association/rescript-core/pull/72 ### Documentation