-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_object_test.ts
65 lines (60 loc) · 1.73 KB
/
error_object_test.ts
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
import { test } from "@cross/test";
import { assertEquals, assertInstanceOf } from "@std/assert";
import { fromErrorObject, toErrorObject } from "./error_object.ts";
class CustomError extends Error {
foo: string;
bar: number;
constructor(message: string, foo: string, bar: number) {
super(message);
this.name = "ThisIsCustomError";
this.foo = foo;
this.bar = bar;
}
}
await test("toErrorObject with Error", () => {
const err = new Error("error");
err.stack = "stack...";
const obj = toErrorObject(err);
assertEquals(obj, {
proto: "Error",
name: "Error",
message: "error",
stack: "stack...",
attributes: {},
});
});
await test("toErrorObject with CustomError", () => {
const err = new CustomError("error", "foo", 10);
err.stack = "stack...";
const obj = toErrorObject(err);
assertEquals(obj, {
proto: "CustomError",
name: "ThisIsCustomError",
message: "error",
stack: "stack...",
attributes: {
foo: "foo",
bar: 10,
},
});
});
await test("fromErrorObject with Error", () => {
const obj = toErrorObject(new Error("error"));
obj.stack = "stack...";
const err = fromErrorObject(obj);
assertInstanceOf(err, Error);
assertEquals(err.name, "Error");
assertEquals(err.message, "error");
assertEquals(err.stack, "stack...");
});
await test("fromErrorObject with CustomError", () => {
const obj = toErrorObject(new CustomError("error", "foo", 10));
obj.stack = "stack...";
const err = fromErrorObject(obj);
assertInstanceOf(err, Error);
assertEquals(err.name, "ThisIsCustomError");
assertEquals(err.message, "error");
assertEquals(err.stack, "stack...");
assertEquals((err as CustomError).foo, "foo");
assertEquals((err as CustomError).bar, 10);
});