Repository navigation
Expand file tree
/
Copy pathbackend.ts
More file actions
135 lines (122 loc) · 4.27 KB
/
Copy pathbackend.ts
File metadata and controls
135 lines (122 loc) · 4.27 KB
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
import { BaseBackend, getCurrentHub } from '@sentry/core';
import { Event, EventHint, Mechanism, Severity, Transport, TransportOptions } from '@sentry/types';
import {
addExceptionMechanism,
addExceptionTypeValue,
Dsn,
extractExceptionKeysForMessage,
isError,
isPlainObject,
normalizeToSize,
SyncPromise,
} from '@sentry/utils';
import { extractStackFromError, parseError, parseStack, prepareFramesForEvent } from './parsers';
import { HTTPSTransport, HTTPTransport } from './transports';
import { NodeOptions } from './types';
/**
* The Sentry Node SDK Backend.
* @hidden
*/
export class NodeBackend extends BaseBackend<NodeOptions> {
/**
* @inheritDoc
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
public eventFromException(exception: any, hint?: EventHint | undefined): PromiseLike<Event> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let ex: any = exception;
const providedMechanism: Mechanism | undefined =
hint && hint.data && (hint.data as { mechanism: Mechanism }).mechanism;
const mechanism: Mechanism = providedMechanism || {
handled: true,
type: 'generic',
};
if (!isError(exception)) {
if (isPlainObject(exception)) {
// This will allow us to group events based on top-level keys
// which is much better than creating new group when any key/value change
const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`;
getCurrentHub().configureScope(scope => {
scope.setExtra('__serialized__', normalizeToSize(exception as Record<string, unknown>));
});
ex = (hint && hint.syntheticException) || new Error(message);
(ex as Error).message = message;
} else {
// This handles when someone does: `throw "something awesome";`
// We use synthesized Error here so we can extract a (rough) stack trace.
ex = (hint && hint.syntheticException) || new Error(exception as string);
(ex as Error).message = exception;
}
mechanism.synthetic = true;
}
return new SyncPromise<Event>((resolve, reject) =>
parseError(ex as Error, this._options)
.then(event => {
addExceptionTypeValue(event, undefined, undefined);
addExceptionMechanism(event, mechanism);
resolve({
...event,
event_id: hint && hint.event_id,
});
})
.then(null, reject),
);
}
/**
* @inheritDoc
*/
public eventFromMessage(
message: string,
level: Severity = Severity.Info,
hint?: EventHint | undefined,
): PromiseLike<Event> {
const event: Event = {
event_id: hint && hint.event_id,
level,
message,
};
return new SyncPromise<Event>(resolve => {
if (this._options.attachStacktrace && hint && hint.syntheticException) {
const stack = hint.syntheticException ? extractStackFromError(hint.syntheticException) : [];
void parseStack(stack, this._options)
.then(frames => {
event.stacktrace = {
frames: prepareFramesForEvent(frames),
};
resolve(event);
})
.then(null, () => {
resolve(event);
});
} else {
resolve(event);
}
});
}
/**
* @inheritDoc
*/
protected _setupTransport(): Transport {
if (!this._options.dsn) {
// We return the noop transport here in case there is no Dsn.
return super._setupTransport();
}
const dsn = new Dsn(this._options.dsn);
const transportOptions: TransportOptions = {
...this._options.transportOptions,
...(this._options.httpProxy && { httpProxy: this._options.httpProxy }),
...(this._options.httpsProxy && { httpsProxy: this._options.httpsProxy }),
...(this._options.caCerts && { caCerts: this._options.caCerts }),
dsn: this._options.dsn,
tunnel: this._options.tunnel,
_metadata: this._options._metadata,
};
if (this._options.transport) {
return new this._options.transport(transportOptions);
}
if (dsn.protocol === 'http') {
return new HTTPTransport(transportOptions);
}
return new HTTPSTransport(transportOptions);
}
}