Repository navigation
Expand file tree
/
Copy pathaggregate-errors.ts
More file actions
133 lines (122 loc) · 4.17 KB
/
Copy pathaggregate-errors.ts
File metadata and controls
133 lines (122 loc) · 4.17 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
import type { ExtendedError } from '../types/error';
import type { Event, EventHint } from '../types/event';
import type { Exception } from '../types/exception';
import type { StackParser } from '../types/stacktrace';
import { isInstanceOf } from './is';
/**
* Creates exceptions inside `event.exception.values` for errors that are nested on properties based on the `key` parameter.
*/
export function applyAggregateErrorsToEvent(
exceptionFromErrorImplementation: (stackParser: StackParser, ex: Error) => Exception,
parser: StackParser,
key: string,
limit: number,
event: Event,
hint?: EventHint,
): void {
if (!event.exception?.values || !hint || !isInstanceOf(hint.originalException, Error)) {
return;
}
// Generally speaking the last item in `event.exception.values` is the exception originating from the original Error
const originalException: Exception | undefined =
event.exception.values.length > 0 ? event.exception.values[event.exception.values.length - 1] : undefined;
// We only create exception grouping if there is an exception in the event.
if (originalException) {
event.exception.values = aggregateExceptionsFromError(
exceptionFromErrorImplementation,
parser,
limit,
hint.originalException as ExtendedError,
key,
event.exception.values,
originalException,
0,
);
}
}
function aggregateExceptionsFromError(
exceptionFromErrorImplementation: (stackParser: StackParser, ex: Error) => Exception,
parser: StackParser,
limit: number,
error: ExtendedError,
key: string,
prevExceptions: Exception[],
exception: Exception,
exceptionId: number,
): Exception[] {
if (prevExceptions.length >= limit + 1) {
return prevExceptions;
}
let newExceptions = [...prevExceptions];
// Recursively call this function in order to walk down a chain of errors
if (isInstanceOf(error[key], Error)) {
applyExceptionGroupFieldsForParentException(exception, exceptionId, error);
const newException = exceptionFromErrorImplementation(parser, error[key] as Error);
const newExceptionId = newExceptions.length;
applyExceptionGroupFieldsForChildException(newException, key, newExceptionId, exceptionId);
newExceptions = aggregateExceptionsFromError(
exceptionFromErrorImplementation,
parser,
limit,
error[key] as ExtendedError,
key,
[newException, ...newExceptions],
newException,
newExceptionId,
);
}
// This will create exception grouping for AggregateErrors
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError
if (isExceptionGroup(error)) {
error.errors.forEach((childError, i) => {
if (isInstanceOf(childError, Error)) {
applyExceptionGroupFieldsForParentException(exception, exceptionId, error);
const newException = exceptionFromErrorImplementation(parser, childError as Error);
const newExceptionId = newExceptions.length;
applyExceptionGroupFieldsForChildException(newException, `errors[${i}]`, newExceptionId, exceptionId);
newExceptions = aggregateExceptionsFromError(
exceptionFromErrorImplementation,
parser,
limit,
childError as ExtendedError,
key,
[newException, ...newExceptions],
newException,
newExceptionId,
);
}
});
}
return newExceptions;
}
function isExceptionGroup(error: ExtendedError): error is ExtendedError & { errors: unknown[] } {
return Array.isArray(error.errors);
}
function applyExceptionGroupFieldsForParentException(
exception: Exception,
exceptionId: number,
error: ExtendedError,
): void {
exception.mechanism = {
handled: true,
type: 'auto.core.linked_errors',
...(isExceptionGroup(error) && { is_exception_group: true }),
...exception.mechanism,
exception_id: exceptionId,
};
}
function applyExceptionGroupFieldsForChildException(
exception: Exception,
source: string,
exceptionId: number,
parentId: number | undefined,
): void {
exception.mechanism = {
handled: true,
...exception.mechanism,
type: 'chained',
source,
exception_id: exceptionId,
parent_id: parentId,
};
}