-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathBrowserErrorPlugin.test.ts
141 lines (118 loc) · 4.44 KB
/
BrowserErrorPlugin.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
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
import { describe, test } from "@jest/globals";
import { expect } from "expect";
import { ErrorInfo, Event, EventContext, EventPluginContext, ExceptionlessClient, KnownEventDataKeys } from "@exceptionless/core";
import { CapturedExceptions } from "./../../../core/test/plugins/default/exceptions.js";
import { BrowserErrorPlugin } from "../../src/plugins/BrowserErrorPlugin.js";
class BaseTestError extends Error {
public name = "NotImplementedError";
public someProperty = "Test";
}
class DerivedTestError extends BaseTestError {
public someOtherProperty = "Test2";
}
describe("BrowserErrorPlugin", () => {
const plugin = new BrowserErrorPlugin();
let context: EventPluginContext;
beforeEach(() => {
plugin.parse = (exception: Error): Promise<ErrorInfo> => {
return Promise.resolve(<ErrorInfo>{
type: exception.name,
message: exception.message,
stack_trace: [],
modules: []
});
};
const client: ExceptionlessClient = new ExceptionlessClient();
const event: Event = {
data: {}
};
context = new EventPluginContext(client, event, new EventContext());
});
function processError(error: Error | string | unknown): Promise<void> {
const exception = throwAndCatch(error);
context.eventContext.setException(exception);
return plugin.run(context);
}
describe("additional data", () => {
describeForCapturedExceptions((exception) => {
test("should ignore default error properties", async () => {
context.eventContext.setException(exception as Error);
await plugin.run(context);
const additionalData = getAdditionalData(context.event);
expect(additionalData).toBeUndefined();
});
});
test("should add error cause", async () => {
const error = {
someProperty: "Test"
};
await processError(new Error("Error With Cause", { cause: error }));
const additionalData = getAdditionalData(context.event);
expect(additionalData).not.toBeNull();
expect(additionalData?.cause).toStrictEqual(error);
});
test("should add custom properties to additional data", async () => {
const error = {
someProperty: "Test"
};
await processError(error);
const additionalData = getAdditionalData(context.event);
expect(additionalData).not.toBeNull();
expect(additionalData?.someProperty).toBe("Test");
});
test("should support custom exception types", async () => {
await processError(new BaseTestError());
const additionalData = getAdditionalData(context.event);
expect(additionalData).not.toBeNull();
expect(additionalData?.someProperty).toBe("Test");
});
test("should support inherited properties", async () => {
await processError(new DerivedTestError());
const additionalData = getAdditionalData(context.event);
expect(additionalData).not.toBeNull();
expect(additionalData?.someProperty).toBe("Test");
expect(additionalData?.someOtherProperty).toBe("Test2");
});
test("shouldn't set empty additional data", async () => {
await processError({});
const additionalData = getAdditionalData(context.event);
expect(additionalData).toBeUndefined();
});
test("should ignore functions", async () => {
class ErrorWithFunction extends Error {
constructor() {
super("Error with function");
}
public someFunction(): number {
return 5;
}
}
const exception = new ErrorWithFunction();
context.eventContext.setException(exception);
await plugin.run(context);
const additionalData = getAdditionalData(context.event);
expect(additionalData).toBeUndefined();
});
});
});
function describeForCapturedExceptions(specDefinitions: (exception: Error | unknown) => void) {
const exceptions = <Record<string, unknown>>CapturedExceptions;
const keys = Object.getOwnPropertyNames(exceptions);
keys.forEach((key) => {
describe(key, () => specDefinitions(exceptions[key]));
});
}
function getError(event: Event): ErrorInfo | undefined {
return event?.data?.[KnownEventDataKeys.Error];
}
function getAdditionalData(event: Event): Record<string, unknown> | undefined {
const error = getError(event);
return error?.data?.["@ext"] as Record<string, unknown>;
}
function throwAndCatch(error: Error | string | unknown): Error {
try {
throw error;
} catch (exception) {
return exception as Error;
}
}