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
|
{
// Plain-old Error object
let error = new Error("foobar");
let report = createErrorReport(error);
assertEq(report.toStringResult, "Error: foobar");
assertEq(report.name, "Error");
assertEq(report.message, "foobar");
}
{
// Error object with overwritten properties
let error = new Error("foobar");
error.name = "MyError";
error.message = "here";
let report = createErrorReport(error);
assertEq(report.toStringResult, "MyError: here");
assertEq(report.name, "Error");
assertEq(report.message, "here");
}
{
// Plain object, which doesn't quack (enough) like an Error
let obj = {name: "foo", message: "bar"};
let report = createErrorReport(obj);
assertEq(report.toStringResult, "uncaught exception: [object Object]");
assertEq(report.name, "");
assertEq(report.message, "uncaught exception: [object Object]");
}
{
// Duck-typed error object
let obj = {name: "foo", message: "bar", fileName: "test", lineNumber: 0};
let report = createErrorReport(obj);
assertEq(report.toStringResult, "foo: bar");
assertEq(report.name, "");
assertEq(report.message, "foo: bar");
}
{
// toString failure
let obj = {toString() { throw "haha"; }};
let report = createErrorReport(obj);
assertEq(report.toStringResult, "uncaught exception: unknown (can't convert to string)");
assertEq(report.name, "");
assertEq(report.message, "uncaught exception: unknown (can't convert to string)");
}
|