-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpigeon-backend.js
277 lines (241 loc) · 8.08 KB
/
pigeon-backend.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const {BaseBackend, getCurrentHub, API} = require("@sentry/core")
const {
addExceptionTypeValue,
isError,
isPlainObject,
keysToEventMessage,
normalizeToSize,
SyncPromise
} = require("@sentry/utils")
const { StackFrame, Status } = require("@sentry/types")
const stacktrace = require('stack-trace')
// Use fetch in dev, not required in prod where workers provides it
// const fetch = require('node-fetch');
class PigeonBackend extends BaseBackend {
constructor(O) {
super(O);
this._setupTransport();
}
_setupTransport() {
if (!this._options.dsn) {
// We return the noop transport here in case there is no Dsn.
return super._setupTransport();
}
const transportOptions = {
...this._options.transportOptions,
dsn: this._options.dsn,
};
if (this._options.transport) {
return new this._options.transport(transportOptions);
}
return new PigeonTransport(transportOptions);
}
eventFromMessage(message, level, hint) {
const event = {
event_id: hint && hint.event_id,
level,
message,
};
// if (this._options.attachStacktrace && hint && hint.syntheticException) {
// const stacktrace = _computeStackTrace(hint.syntheticException);
// const frames = this.prepareFramesForEvent(stacktrace.stack);
// event.stacktrace = {
// frames,
// };
// }
return new SyncPromise(resolve => {
resolve(event);
});
}
/**
* Note - mostly based on the node backend
* @inheritDoc
*/
eventFromException(exception, hint) {
let ex = exception;
const mechanism = {
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 keys = Object.keys(exception).sort();
const message = `Non-Error exception captured with keys: ${keysToEventMessage(keys)}`;
getCurrentHub().configureScope(scope => {
scope.setExtra('__serialized__', normalizeToSize(exception));
});
ex = (hint && hint.syntheticException) || new Error(message);
(ex).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);
}
mechanism.synthetic = true;
}
return new SyncPromise((resolve, reject) =>
this.parseError(ex, this._options)
.then(event => {
addExceptionTypeValue(event, undefined, undefined, mechanism);
resolve({
...event,
event_id: hint && hint.event_id,
});
})
.catch(reject),
);
}
sendEvent(event) {
return this._transport.sendEvent(event);
}
/**
* From @sentry/node
* @param error {ExtendedError}
* @param options {NodeOptions?}
* @return SyncPromise
*/
parseError(error, options) {
return new SyncPromise(resolve =>
this.getExceptionFromError(error, options).then((exception) => {
resolve({
exception: {
values: [exception],
},
});
}),
);
}
/**
* From @sentry/node, but using the non-patched stacktrace library
* @param error {Error}
* @returns {StackFrame[]}
*/
extractStackFromError(error) {
const stack = stacktrace.parse(error);
if (!stack) {
return [];
}
return stack;
}
/**
* From @sentry/node
* @param stack {StackFrame[]}
* @returns {StackFrame[]}
*/
prepareFramesForEvent(stack) {
if (!stack || !stack.length) {
return [];
}
let localStack = stack;
const firstFrameFunction = localStack[0].function || '';
if (firstFrameFunction.includes('captureMessage') || firstFrameFunction.includes('captureException')) {
localStack = localStack.slice(1);
}
// The frame where the crash happened, should be the last entry in the array
return localStack.reverse();
}
/**
*
* @param error {Error}
* @param options
* @return SyncPromise
*/
getExceptionFromError(error, options) {
const name = error.name || error.constructor.name;
const stack = this.extractStackFromError(error);
return new SyncPromise(resolve =>
this.parseStack(stack, options).then(frames => {
const result = {
stacktrace: {
frames: this.prepareFramesForEvent(frames),
},
type: name,
value: error.message,
};
resolve(result);
}),
);
}
/**
*
* @param frame {stacktrace.StackFrame}
* @return {string|*|string}
*/
getFunction(frame) {
try {
return frame.functionName || `${frame.typeName}.${frame.methodName || '<anonymous>'}`;
} catch (e) {
// This seems to happen sometimes when using 'use strict',
// stemming from `getTypeName`.
// [TypeError: Cannot read property 'constructor' of undefined]
return '<anonymous>';
}
}
/**
* From @sentry/node but pulled out a bunch of code related to node_modules
*
* @param stack {stacktrace.StackFrame[]}
* @param options
* @return SyncPromise
*/
parseStack(stack, options) {
const frames = stack.map(frame => {
const parsedFrame = {
colno: frame.columnNumber,
filename: frame.fileName ? 'app:///' + frame.fileName : null,
function: this.getFunction(frame),
lineno: frame.lineNumber,
platform: 'javascript'
};
return parsedFrame;
});
return SyncPromise.resolve(frames);
}
}
/**
* Similar to BaseTransport in @sentry/browser, but didn't want to import that whole
* module.
*/
class PigeonTransport {
constructor(options) {
const api = new API(options.dsn);
this.sdk_name = options.sdk_name;
this.sdk_version = options.sdk_version;
this.workers_event = options.workers_event;
this.url = api.getStoreEndpoint();
this.headers = Object.assign(
api.getRequestHeaders(this.sdk_name, this.sdk_version),
options.headers,
);
}
//sendEvent(event: Event): Promise<Response>;
sendEvent(event) {
const defaultOptions = {
body: JSON.stringify(event),
method: 'POST',
headers: {
"User-Agent": `${this.sdk_name}/${this.sdk_version}`,
...this.headers,
}
};
const fetchPromise = fetch(this.url, defaultOptions).then(response => ({
status: Status.fromHttpCode(response.status),
}));
this.workers_event.waitUntil(fetchPromise);
return fetchPromise;
}
/**
* Call this function to wait until all pending requests have been sent.
* Note, in workers we don't use a buffer so this will always return a promise
* that resolves to true.
*
* @param timeout Number time in ms to wait until the buffer is drained.
*/
//close(timeout?: number): Promise<boolean>;
close(timeout) {
return Promise.resolve(true);
}
}
module.exports = PigeonBackend