-
Notifications
You must be signed in to change notification settings - Fork 29
docs for Error #46
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
docs for Error #46
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/*** | ||
Functions for working with JavaScript exceptions. | ||
|
||
See [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) on MDN. | ||
*/ | ||
|
||
/** Represents a JavaScript exception. */ | ||
type t = Js.Exn.t | ||
|
||
external fromException: exn => option<t> = "?as_js_exn" | ||
|
||
/** | ||
Turns an `Error.t` into an `exn`. | ||
|
||
## Examples | ||
```rescript | ||
let error = Error.make("Something went wrong.") | ||
|
||
let asExn = error->Error.toException // `asExn` is now type `exn` | ||
``` | ||
*/ | ||
external toException: t => exn = "%identity" | ||
|
||
/** | ||
`stack(error)` retrieves the `stack` property of the error, if it exists. The stack is a list of what functions were called, and what files they are defined in, prior to the error happening. | ||
|
||
See [`Error.prototype.stack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack) on MDN. | ||
|
||
## Example | ||
```rescript | ||
Console.log(someError->Error.stack) // Logs `stack` if it exists on `someError` | ||
``` | ||
*/ | ||
@get | ||
external stack: t => option<string> = "stack" | ||
|
||
/** | ||
`message(error)` retrieves the `message` property of the error, if it exists. | ||
|
||
See [`Error.prototype.message`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message) on MDN. | ||
|
||
## Example | ||
```rescript | ||
let error = Error.SyntaxError.make("Some message here") | ||
Console.log(error->Error.message) // Logs "Some message here" to the console | ||
``` | ||
*/ | ||
@get | ||
external message: t => option<string> = "message" | ||
|
||
/** | ||
`name(error)` retrieves the `name` property of the error, if it exists. | ||
|
||
See [`Error.prototype.name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name) on MDN. | ||
|
||
## Example | ||
```rescript | ||
let error = Error.SyntaxError.make("Some message here") | ||
Console.log(error->Error.name) // Logs "SyntaxError" to the console | ||
``` | ||
*/ | ||
@get | ||
external name: t => option<string> = "name" | ||
|
||
/** | ||
`fileName(error)` retrieves the `fileName` property of the error, if it exists. | ||
|
||
See [`Error.prototype.fileName`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/fileName) on MDN. | ||
*/ | ||
@get | ||
external fileName: t => option<string> = "fileName" | ||
|
||
/** | ||
`make(message)` creates a new error, setting its `message` to the provided value. | ||
|
||
See [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error) on MDN. | ||
|
||
## Example | ||
```rescript | ||
let error = Error.make("Some message here") | ||
Console.log(error->Error.message) // Logs "Some message here" to the console | ||
Console.log(error->Error.name) // Logs "Error" to the console, because this is a regular error | ||
``` | ||
*/ | ||
@new | ||
external make: string => t = "Error" | ||
|
||
module EvalError: { | ||
/** | ||
Creates a new `EvalError` with the provided `message`. | ||
|
||
See [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError) on MDN. | ||
*/ | ||
@new | ||
external make: string => t = "EvalError" | ||
} | ||
module RangeError: { | ||
/** | ||
Creates a new `RangeError` with the provided `message`. | ||
|
||
See [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError) on MDN. | ||
*/ | ||
@new | ||
external make: string => t = "RangeError" | ||
} | ||
module ReferenceError: { | ||
/** | ||
Creates a new `ReferenceError` with the provided `message`. | ||
|
||
See [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) on MDN. | ||
*/ | ||
@new | ||
external make: string => t = "ReferenceError" | ||
} | ||
module SyntaxError: { | ||
/** | ||
Creates a new `SyntaxError` with the provided `message`. | ||
|
||
See [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) on MDN. | ||
*/ | ||
@new | ||
external make: string => t = "SyntaxError" | ||
} | ||
module TypeError: { | ||
/** | ||
Creates a new `TypeError` with the provided `message`. | ||
|
||
See [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) on MDN. | ||
*/ | ||
@new | ||
external make: string => t = "TypeError" | ||
} | ||
module URIError: { | ||
/** | ||
Creates a new `URIError` with the provided `message`. | ||
|
||
See [`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError) on MDN. | ||
*/ | ||
@new | ||
external make: string => t = "URIError" | ||
} | ||
|
||
/** | ||
Raises (throws in JavaScript language) the provided `Error.t`, which will stop execution. | ||
|
||
## Examples | ||
```rescript | ||
let error = Error.make("Everything is upside down.") | ||
|
||
if 5 > 10 { | ||
error->Error.raise | ||
} else { | ||
Console.log("Phew, sanity still rules.") | ||
} | ||
``` | ||
*/ | ||
external raise: t => 'a = "%raise" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving this right now because I quite frankly don't fully understand why it does, or rather when I'd use it.