-
Notifications
You must be signed in to change notification settings - Fork 463
/
Copy pathStdlib_Error.resi
190 lines (154 loc) · 5.24 KB
/
Stdlib_Error.resi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/***
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 = Stdlib_Exn.t
let fromException: exn => option<t>
/**
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
let error = Error.make("error")
Console.log(error->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"
/**
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
try {
Error.panic("Uh oh. This was unexpected!")
} catch {
| Exn.Error(obj) =>
switch Exn.message(obj) {
| Some(m) => assert(m == "Panic! Uh oh. This was unexpected!")
| None => assert(false)
}
| _ => assert(false)
}
```
*/
let panic: string => 'a
/**
`ignore(error)` ignores the provided error and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects)
without having to store or process it further.
*/
external ignore: t => unit = "%ignore"