-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathclient.ts
103 lines (86 loc) · 2.61 KB
/
client.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
import type {
ClientOptions,
Event,
EventHint,
Integration,
Outcome,
ParameterizedString,
Session,
SeverityLevel,
} from '../../src/types-hoist';
import { Client } from '../../src/client';
import { initAndBind } from '../../src/sdk';
import { createTransport } from '../../src/transports/base';
import { resolvedSyncPromise } from '../../src/utils-hoist/syncpromise';
export function getDefaultTestClientOptions(options: Partial<TestClientOptions> = {}): TestClientOptions {
return {
integrations: [],
sendClientReports: true,
transport: () =>
createTransport(
{
recordDroppedEvent: () => undefined,
}, // noop
_ => resolvedSyncPromise({}),
),
stackParser: () => [],
...options,
};
}
export interface TestClientOptions extends ClientOptions {
test?: boolean;
mockInstallFailure?: boolean;
enableSend?: boolean;
defaultIntegrations?: Integration[] | false;
}
export class TestClient extends Client<TestClientOptions> {
public static instance?: TestClient;
public static sendEventCalled?: (event: Event) => void;
public event?: Event;
public session?: Session;
public constructor(options: TestClientOptions) {
super(options);
TestClient.instance = this;
}
public eventFromException(exception: any): PromiseLike<Event> {
const event: Event = {
exception: {
values: [
{
type: exception.name,
value: exception.message,
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
},
],
},
};
const frames = this._options.stackParser(exception.stack || '', 1);
if (frames.length && event.exception?.values?.[0]) {
event.exception.values[0] = { ...event.exception.values[0], stacktrace: { frames } };
}
return resolvedSyncPromise(event);
}
public eventFromMessage(message: ParameterizedString, level: SeverityLevel = 'info'): PromiseLike<Event> {
return resolvedSyncPromise({ message, level });
}
public sendEvent(event: Event, hint?: EventHint): void {
this.event = event;
if (this._options.enableSend) {
super.sendEvent(event, hint);
return;
}
// In real life, this will get deleted as part of envelope creation.
delete event.sdkProcessingMetadata;
TestClient.sendEventCalled?.(event);
}
public sendSession(session: Session): void {
this.session = session;
}
// Public proxy for protected method
public _clearOutcomes(): Outcome[] {
return super._clearOutcomes();
}
}
export function init(options: TestClientOptions): void {
initAndBind(TestClient, options);
}