diff --git a/CHANGELOG.md b/CHANGELOG.md index b28f5fd1..1febff2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Add `make`, `fromInitializer`, `findMap`, `keepSome`, `reduceRight` and `reduceRightWithIndex`. https://github.com/rescript-association/rescript-core/pull/49 - Remove `reduceReverse` in favor of `reduceRight`. https://github.com/rescript-association/rescript-core/pull/49 - Fixed type signatures of `reduce` and `reduceWithIndex`. https://github.com/rescript-association/rescript-core/pull/49 +- Add `panic`/`Error.panic`. https://github.com/rescript-association/rescript-core/pull/72 ### Documentation 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 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 index 59a2f4e7..64ebfbc1 100644 --- a/test/TestSuite.mjs +++ b/test/TestSuite.mjs @@ -1,5 +1,6 @@ // Generated by ReScript, PLEASE EDIT WITH CARE +import * as ErrorTests from "./ErrorTests.mjs"; import * as ArrayTests from "./ArrayTests.mjs"; import * as PromiseTest from "./PromiseTest.mjs"; @@ -19,6 +20,8 @@ var Catching = PromiseTest.Catching; var Concurrently = PromiseTest.Concurrently; +var panicTest = ErrorTests.panicTest; + var eq = ArrayTests.eq; export { @@ -30,6 +33,7 @@ export { Rejection , Catching , Concurrently , + panicTest , eq , } /* ArrayTests Not a pure module */ diff --git a/test/TestSuite.res b/test/TestSuite.res index a376fea1..e093f677 100644 --- a/test/TestSuite.res +++ b/test/TestSuite.res @@ -1,2 +1,3 @@ include PromiseTest +include ErrorTests include ArrayTests