-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
linkederrors.ts
97 lines (86 loc) · 2.6 KB
/
linkederrors.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
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import { Event, EventHint, Exception, ExtendedError, Integration } from '@sentry/types';
import { isInstanceOf, SyncPromise } from '@sentry/utils';
import { getExceptionFromError } from '../parsers';
const DEFAULT_KEY = 'cause';
const DEFAULT_LIMIT = 5;
/** Adds SDK info to an event. */
export class LinkedErrors implements Integration {
/**
* @inheritDoc
*/
public readonly name: string = LinkedErrors.id;
/**
* @inheritDoc
*/
public static id: string = 'LinkedErrors';
/**
* @inheritDoc
*/
private readonly _key: string;
/**
* @inheritDoc
*/
private readonly _limit: number;
/**
* @inheritDoc
*/
public constructor(options: { key?: string; limit?: number } = {}) {
this._key = options.key || DEFAULT_KEY;
this._limit = options.limit || DEFAULT_LIMIT;
}
/**
* @inheritDoc
*/
public setupOnce(): void {
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
const self = getCurrentHub().getIntegration(LinkedErrors);
if (self) {
const handler = self._handler && self._handler.bind(self);
return typeof handler === 'function' ? handler(event, hint) : event;
}
return event;
});
}
/**
* @inheritDoc
*/
private _handler(event: Event, hint?: EventHint): PromiseLike<Event> {
if (!event.exception || !event.exception.values || !hint || !isInstanceOf(hint.originalException, Error)) {
return SyncPromise.resolve(event);
}
return new SyncPromise<Event>(resolve => {
this._walkErrorTree(hint.originalException as Error, this._key)
.then((linkedErrors: Exception[]) => {
if (event && event.exception && event.exception.values) {
event.exception.values = [...linkedErrors, ...event.exception.values];
}
resolve(event);
})
.then(null, () => {
resolve(event);
});
});
}
/**
* @inheritDoc
*/
private _walkErrorTree(error: ExtendedError, key: string, stack: Exception[] = []): PromiseLike<Exception[]> {
if (!isInstanceOf(error[key], Error) || stack.length + 1 >= this._limit) {
return SyncPromise.resolve(stack);
}
return new SyncPromise<Exception[]>((resolve, reject) => {
getExceptionFromError(error[key])
.then((exception: Exception) => {
this._walkErrorTree(error[key], key, [exception, ...stack])
.then(resolve)
.then(null, () => {
reject();
});
})
.then(null, () => {
reject();
});
});
}
}