Skip to content

Add panic function #72

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

Merged
merged 5 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/Core__Error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ var $$TypeError = {};

var $$URIError = {};

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

export {
$$EvalError ,
$$RangeError ,
$$ReferenceError ,
$$SyntaxError ,
$$TypeError ,
$$URIError ,
panic ,
}
/* No side effect */
2 changes: 2 additions & 0 deletions src/Core__Error.res
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ module URIError = {
}

external raise: t => 'a = "%raise"

let panic = msg => make(j`Panic! $msg`)->raise
14 changes: 14 additions & 0 deletions src/Core__Error.resi
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/RescriptCore.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Core__Error from "./Core__Error.mjs";

var $$Array;

Expand Down Expand Up @@ -93,6 +94,8 @@ var List;

var Result;

var panic = Core__Error.panic;

export {
$$Array ,
Console ,
Expand Down Expand Up @@ -140,5 +143,6 @@ export {
$$Option ,
List ,
Result ,
panic ,
}
/* No side effect */
2 changes: 2 additions & 0 deletions src/RescriptCore.res
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 39 additions & 0 deletions test/ErrorTests.mjs
Original file line number Diff line number Diff line change
@@ -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 */
11 changes: 11 additions & 0 deletions test/ErrorTests.res
Original file line number Diff line number Diff line change
@@ -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()
35 changes: 35 additions & 0 deletions test/TestSuite.mjs
Original file line number Diff line number Diff line change
@@ -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 */
2 changes: 2 additions & 0 deletions test/TestSuite.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include PromiseTest
include ErrorTests