-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathErrorTests.res
41 lines (36 loc) · 1.01 KB
/
ErrorTests.res
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
open RescriptCore
let panicTest = () => {
let caught = try panic("uh oh") catch {
| Exn.Error(err) => Error.message(err)
}
Test.run(__POS_OF__("Should resolve test"), caught, \"==", Some("Panic! uh oh"))
}
panicTest()
// This test case is based on catching authentication errors from the Firebase
// SDK. Errors are subclassed from the Error object and contain custom
// properties like "code".
let catchCustomError = () => {
let authenticationError = Error.make("authentication error")
let codeCaught = ref(None)
Object.set(authenticationError->Obj.magic, "code", "invalid-password")
try {
authenticationError->Error.raise
} catch {
| _ as exn =>
switch exn->Error.fromException {
| None => raise(exn)
| Some(err) =>
switch err->Error.getUnsafe("code") {
| None => ()
| Some(code) => codeCaught := Some(code)
}
}
}
Test.run(
__POS_OF__("Can access custom code"),
codeCaught.contents,
\"==",
Some("invalid-password"),
)
}
catchCustomError()