Skip to content

Commit 371b8f3

Browse files
authored
docs for Error (#46)
1 parent efc446e commit 371b8f3

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

src/Core__Error.resi

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/***
2+
Functions for working with JavaScript exceptions.
3+
4+
See [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) on MDN.
5+
*/
6+
7+
/** Represents a JavaScript exception. */
8+
type t = Js.Exn.t
9+
10+
external fromException: exn => option<t> = "?as_js_exn"
11+
12+
/**
13+
Turns an `Error.t` into an `exn`.
14+
15+
## Examples
16+
```rescript
17+
let error = Error.make("Something went wrong.")
18+
19+
let asExn = error->Error.toException // `asExn` is now type `exn`
20+
```
21+
*/
22+
external toException: t => exn = "%identity"
23+
24+
/**
25+
`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.
26+
27+
See [`Error.prototype.stack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack) on MDN.
28+
29+
## Example
30+
```rescript
31+
Console.log(someError->Error.stack) // Logs `stack` if it exists on `someError`
32+
```
33+
*/
34+
@get
35+
external stack: t => option<string> = "stack"
36+
37+
/**
38+
`message(error)` retrieves the `message` property of the error, if it exists.
39+
40+
See [`Error.prototype.message`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message) on MDN.
41+
42+
## Example
43+
```rescript
44+
let error = Error.SyntaxError.make("Some message here")
45+
Console.log(error->Error.message) // Logs "Some message here" to the console
46+
```
47+
*/
48+
@get
49+
external message: t => option<string> = "message"
50+
51+
/**
52+
`name(error)` retrieves the `name` property of the error, if it exists.
53+
54+
See [`Error.prototype.name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name) on MDN.
55+
56+
## Example
57+
```rescript
58+
let error = Error.SyntaxError.make("Some message here")
59+
Console.log(error->Error.name) // Logs "SyntaxError" to the console
60+
```
61+
*/
62+
@get
63+
external name: t => option<string> = "name"
64+
65+
/**
66+
`fileName(error)` retrieves the `fileName` property of the error, if it exists.
67+
68+
See [`Error.prototype.fileName`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/fileName) on MDN.
69+
*/
70+
@get
71+
external fileName: t => option<string> = "fileName"
72+
73+
/**
74+
`make(message)` creates a new error, setting its `message` to the provided value.
75+
76+
See [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error) on MDN.
77+
78+
## Example
79+
```rescript
80+
let error = Error.make("Some message here")
81+
Console.log(error->Error.message) // Logs "Some message here" to the console
82+
Console.log(error->Error.name) // Logs "Error" to the console, because this is a regular error
83+
```
84+
*/
85+
@new
86+
external make: string => t = "Error"
87+
88+
module EvalError: {
89+
/**
90+
Creates a new `EvalError` with the provided `message`.
91+
92+
See [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError) on MDN.
93+
*/
94+
@new
95+
external make: string => t = "EvalError"
96+
}
97+
module RangeError: {
98+
/**
99+
Creates a new `RangeError` with the provided `message`.
100+
101+
See [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError) on MDN.
102+
*/
103+
@new
104+
external make: string => t = "RangeError"
105+
}
106+
module ReferenceError: {
107+
/**
108+
Creates a new `ReferenceError` with the provided `message`.
109+
110+
See [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) on MDN.
111+
*/
112+
@new
113+
external make: string => t = "ReferenceError"
114+
}
115+
module SyntaxError: {
116+
/**
117+
Creates a new `SyntaxError` with the provided `message`.
118+
119+
See [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) on MDN.
120+
*/
121+
@new
122+
external make: string => t = "SyntaxError"
123+
}
124+
module TypeError: {
125+
/**
126+
Creates a new `TypeError` with the provided `message`.
127+
128+
See [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) on MDN.
129+
*/
130+
@new
131+
external make: string => t = "TypeError"
132+
}
133+
module URIError: {
134+
/**
135+
Creates a new `URIError` with the provided `message`.
136+
137+
See [`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError) on MDN.
138+
*/
139+
@new
140+
external make: string => t = "URIError"
141+
}
142+
143+
/**
144+
Raises (throws in JavaScript language) the provided `Error.t`, which will stop execution.
145+
146+
## Examples
147+
```rescript
148+
let error = Error.make("Everything is upside down.")
149+
150+
if 5 > 10 {
151+
error->Error.raise
152+
} else {
153+
Console.log("Phew, sanity still rules.")
154+
}
155+
```
156+
*/
157+
external raise: t => 'a = "%raise"

0 commit comments

Comments
 (0)