this repo has no description
1import { getCurrentHub, addGlobalEventProcessor } from '@sentry/core';
2import { isInstanceOf } from '@sentry/utils';
3import { exceptionFromError } from '../eventbuilder.js';
4
5const DEFAULT_KEY = 'cause';
6const DEFAULT_LIMIT = 5;
7
8/** Adds SDK info to an event. */
9class LinkedErrors {
10 /**
11 * @inheritDoc
12 */
13 static __initStatic() {this.id = 'LinkedErrors';}
14
15 /**
16 * @inheritDoc
17 */
18 __init() {this.name = LinkedErrors.id;}
19
20 /**
21 * @inheritDoc
22 */
23
24 /**
25 * @inheritDoc
26 */
27
28 /**
29 * @inheritDoc
30 */
31 constructor(options = {}) {LinkedErrors.prototype.__init.call(this);
32 this._key = options.key || DEFAULT_KEY;
33 this._limit = options.limit || DEFAULT_LIMIT;
34 }
35
36 /**
37 * @inheritDoc
38 */
39 setupOnce() {
40 const client = getCurrentHub().getClient();
41 if (!client) {
42 return;
43 }
44 addGlobalEventProcessor((event, hint) => {
45 const self = getCurrentHub().getIntegration(LinkedErrors);
46 return self ? _handler(client.getOptions().stackParser, self._key, self._limit, event, hint) : event;
47 });
48 }
49} LinkedErrors.__initStatic();
50
51/**
52 * @inheritDoc
53 */
54function _handler(
55 parser,
56 key,
57 limit,
58 event,
59 hint,
60) {
61 if (!event.exception || !event.exception.values || !hint || !isInstanceOf(hint.originalException, Error)) {
62 return event;
63 }
64 const linkedErrors = _walkErrorTree(parser, limit, hint.originalException , key);
65 event.exception.values = [...linkedErrors, ...event.exception.values];
66 return event;
67}
68
69/**
70 * JSDOC
71 */
72function _walkErrorTree(
73 parser,
74 limit,
75 error,
76 key,
77 stack = [],
78) {
79 if (!isInstanceOf(error[key], Error) || stack.length + 1 >= limit) {
80 return stack;
81 }
82 const exception = exceptionFromError(parser, error[key]);
83 return _walkErrorTree(parser, limit, error[key], key, [exception, ...stack]);
84}
85
86export { LinkedErrors, _handler, _walkErrorTree };
87//# sourceMappingURL=linkederrors.js.map