This repository was archived by the owner on Oct 29, 2024. It is now read-only.
js_exn: add brief explanations and links to MDN.#206
Open
jdeisenberg wants to merge 1 commit intorescript-association:masterfrom
Open
js_exn: add brief explanations and links to MDN.#206jdeisenberg wants to merge 1 commit intorescript-association:masterfrom
jdeisenberg wants to merge 1 commit intorescript-association:masterfrom
Conversation
ryyppy
reviewed
May 27, 2020
| let asJsExn: exn => option(t); | ||
| ``` | ||
|
|
||
| Returns `Some(exn)` as a JavaScript exception if its argument is an OCaml `Error`, `None` otherwise. *Someone needs to check this; I am not really sure this is an accurate description.* |
Member
There was a problem hiding this comment.
Suggested change
| Returns `Some(exn)` as a JavaScript exception if its argument is an OCaml `Error`, `None` otherwise. *Someone needs to check this; I am not really sure this is an accurate description.* | |
| Returns `Some(jsExn)` if the input argument `exn` is an actual JS exception, and returns `None` if `exn` is a native Reason exception. | |
| ```re example | |
| let throwJsException: unit => 'a = [%raw {|function() { throw "hello"; }|}]; | |
| // This will log out "hello", since a JS exception was thrown | |
| try (throwJsException()) { | |
| | myExn => switch(myExn->Js.Exn.asJsExn) { | |
| | Some(jsExn) => jsExn->Js.log | |
| | None => Js.log("this is a Reason exception") | |
| } | |
| }; | |
| exception MyError; | |
| // This will log out "this is a Reason exception", since our raised error is a native Reason exception | |
| try (raise(MyError)) { | |
| | myExn => switch(myExn->Js.Exn.asJsExn) { | |
| | Some(jsExn) => jsExn->Js.log | |
| | None => Js.log("this is a Reason exception") | |
| } | |
| }; | |
| ``` |
(Thanks to @cristianoc for explaining this feature)
Member
There was a problem hiding this comment.
after further discussion, this function seems rather redundant, since the same functionality can be achieved via pattern matching:
try(throwJsException()) {
| Js.Exn.Error(_) => Js.log("Js exception");
| _ => Js.log("Some Reason exception")
}
Contributor
Author
There was a problem hiding this comment.
How about combining these? Change the explanation to:
Returns
Some(jsExn)if the input argumentexnis an actual JS exception, and returnsNoneifexnis a native Reason exception.
and then add:
Rather than using this function, you can get the same functionality with pattern matching:
with your example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please check the documentation for
asJsExn; I am not sure I read the code for this correctly (nor am i sure how to use it).