/
error.js
201 lines (170 loc) · 7.4 KB
/
error.js
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
import structuredClone from "../index.js";
const { describe, it, before } = intern.getPlugin("interface.bdd");
const { expect } = intern.getPlugin("chai");
describe("error", function() {
it("supports cloning errors", function() {
const error = new Error("test_error");
const clonedError = structuredClone(error);
expect(clonedError).not.to.equal(error);
expect(clonedError).to.be.an.instanceOf(Error);
expect(clonedError.constructor).to.equal(error.constructor);
expect(clonedError.message).to.equal(error.message);
expect(clonedError.name).to.equal(error.name);
expect(clonedError.stack).to.equal(error.stack);
});
it("Empty Error objects can be cloned", function() {
const error = new Error();
expect(error.hasOwnProperty("message")).to.be.false;
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(Error.prototype);
expect(clonedError.constructor).equal(Error);
expect(clonedError.name).equal("Error");
expect(clonedError.hasOwnProperty("message")).to.be.false;
expect(clonedError.foo).equal(undefined);
});
it("Error objects can be cloned", function() {
const error = Error("some message");
error.foo = "bar";
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(Error.prototype);
expect(clonedError.constructor).equal(Error);
expect(clonedError.name).equal("Error");
expect(clonedError.message).equal("some message");
expect(clonedError.foo).equal(undefined);
});
it("EvalError objects can be cloned", function() {
const error = EvalError("some message");
error.foo = "bar";
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(EvalError.prototype);
expect(clonedError.constructor).equal(EvalError);
expect(clonedError.name).equal("EvalError");
expect(clonedError.message).equal("some message");
expect(clonedError.foo).equal(undefined);
});
it("RangeError objects can be cloned", function() {
const error = RangeError("some message");
error.foo = "bar";
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(RangeError.prototype);
expect(clonedError.constructor).equal(RangeError);
expect(clonedError.name).equal("RangeError");
expect(clonedError.message).equal("some message");
expect(clonedError.foo).equal(undefined);
});
it("ReferenceError objects can be cloned", function() {
const error = ReferenceError("some message");
error.foo = "bar";
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(ReferenceError.prototype);
expect(clonedError.constructor).equal(ReferenceError);
expect(clonedError.name).equal("ReferenceError");
expect(clonedError.message).equal("some message");
expect(clonedError.foo).equal(undefined);
});
it("SyntaxError objects can be cloned", function() {
const error = SyntaxError("some message");
error.foo = "bar";
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(SyntaxError.prototype);
expect(clonedError.constructor).equal(SyntaxError);
expect(clonedError.name).equal("SyntaxError");
expect(clonedError.message).equal("some message");
expect(clonedError.foo).equal(undefined);
});
it("TypeError objects can be cloned", function() {
const error = TypeError("some message");
error.foo = "bar";
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(TypeError.prototype);
expect(clonedError.constructor).equal(TypeError);
expect(clonedError.name).equal("TypeError");
expect(clonedError.message).equal("some message");
expect(clonedError.foo).equal(undefined);
});
it("URIError objects can be cloned", function() {
const error = URIError("some message");
error.foo = "bar";
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(URIError.prototype);
expect(clonedError.constructor).equal(URIError);
expect(clonedError.name).equal("URIError");
expect(clonedError.message).equal("some message");
expect(clonedError.foo).equal(undefined);
});
it("Cloning a modified Error", function() {
const error = URIError("some message");
Object.setPrototypeOf(error, SyntaxError.prototype);
error.message = { toString: () => "another message" };
error.constructor = RangeError;
error.name = "TypeError";
error.foo = "bar";
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(TypeError.prototype);
expect(clonedError.constructor).equal(TypeError);
expect(clonedError.name).equal("TypeError");
expect(clonedError.message).equal("another message");
expect(clonedError.foo).equal(undefined);
});
it("Error.message: getter is ignored when cloning", function() {
const error = Error();
Object.defineProperty(error, "message", { get: () => "hello" });
expect(error.message).equal("hello");
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(Error.prototype);
expect(clonedError.constructor).equal(Error);
expect(clonedError.name).equal("Error");
expect(clonedError.hasOwnProperty("message")).to.be.false;
expect(clonedError.foo).equal(undefined);
});
it("Error.message: undefined property is stringified", function() {
const error = Error();
error.message = undefined;
expect(error.message).equal(undefined);
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(Error.prototype);
expect(clonedError.constructor).equal(Error);
expect(clonedError.name).equal("Error");
expect(clonedError.message).equal("undefined");
expect(clonedError.foo).equal(undefined);
});
describe("DOMException", function() {
before(function(suite) {
if (typeof DOMException === "undefined") {
suite.skip();
}
});
it("objects can be cloned", function() {
const error = new DOMException("some message", "IndexSizeError");
const clonedError = structuredClone(error);
expect(Object.getPrototypeOf(clonedError)).equal(DOMException.prototype);
expect(clonedError.constructor).equal(DOMException);
expect(clonedError.name).equal("IndexSizeError");
expect(clonedError.message).equal("some message");
expect(clonedError.code).equal(DOMException.INDEX_SIZE_ERR);
expect(clonedError.foo).equal(undefined);
});
});
describe("cause", function() {
before(function(suite) {
let supportsCause = false;
new Error("", {
get cause() {
supportsCause = true;
}
});
if (!supportsCause) suite.skip();
});
it("clones error cause", function() {
const error = new Error("test_error", { cause: new Error("cause") });
const clonedError = structuredClone(error);
expect(clonedError).not.to.equal(error);
expect(clonedError.cause).not.to.equal(error.cause);
expect(clonedError.cause).to.be.an.instanceOf(Error);
expect(clonedError.cause.constructor).to.equal(error.cause.constructor);
expect(clonedError.cause.message).to.equal(error.cause.message);
expect(clonedError.cause.name).to.equal(error.cause.name);
expect(clonedError.cause.stack).to.equal(error.cause.stack);
});
});
});