-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
client.ts
79 lines (66 loc) · 2.28 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
import { Session } from '@sentry/hub';
import { Event, Options, Severity, SeverityLevel, Transport } from '@sentry/types';
import { resolvedSyncPromise } from '@sentry/utils';
import { BaseClient } from '../../src/baseclient';
import { initAndBind } from '../../src/sdk';
export interface TestOptions extends Options {
test?: boolean;
mockInstallFailure?: boolean;
enableSend?: boolean;
}
export class TestClient extends BaseClient<TestOptions> {
public static instance?: TestClient;
public static sendEventCalled?: (event: Event) => void;
public event?: Event;
public session?: Session;
public constructor(options: TestOptions) {
super(options);
TestClient.instance = this;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
public eventFromException(exception: any): PromiseLike<Event> {
return resolvedSyncPromise({
exception: {
values: [
{
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
type: exception.name,
value: exception.message,
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
},
],
},
});
}
public eventFromMessage(message: string, level: Severity | SeverityLevel = 'info'): PromiseLike<Event> {
return resolvedSyncPromise({ message, level });
}
public sendEvent(event: Event): void {
this.event = event;
if (this._options.enableSend) {
super.sendEvent(event);
return;
}
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
TestClient.sendEventCalled && TestClient.sendEventCalled(event);
}
public sendSession(session: Session): void {
this.session = session;
}
protected _setupTransport(): Transport {
if (!this._options.dsn) {
// We return the noop transport here in case there is no Dsn.
return super._setupTransport();
}
const transportOptions = this._options.transportOptions
? this._options.transportOptions
: { dsn: this._options.dsn };
if (this._options.transport) {
return new this._options.transport(transportOptions);
}
return super._setupTransport();
}
}
export function init(options: TestOptions): void {
initAndBind(TestClient, options);
}