-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCore__Error.resi
203 lines (163 loc) · 5.66 KB
/
Core__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
191
192
193
194
195
196
197
198
199
200
201
202
203
/***
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
type exn += private Error(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
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"
/** internal use only */
external isCamlExceptionOrOpenVariant: 'a => bool = "?is_extension"
/**
`anyToExnInternal(obj)` will take any value `obj` and wrap it
in a `Error.Error` if given value is not an exn already. If
`obj` is an exn, it will return `obj` without any changes.
This function is mostly useful for cases where you want to unify a type of a value
that potentially is either exn, a JS error, or any other JS value really (e.g. for
a value passed to a Promise.catch callback)
**IMPORTANT**: This is an internal API and may be changed / removed any time in the future.
## Examples
```rescript
switch Error.anyToExnInternal("test") {
| Error.Error(v) =>
switch(Error.message(v)) {
| Some(_) => assert(false)
| None => Console.log2("We will land here", v)
}
| _ => assert(false)
}
```
*/
external anyToExnInternal: 'a => exn = "#wrap_exn"
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
Error.panic("Uh oh. This was unexpected!")
```
*/
let panic: string => 'a