Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/core/src/utils/aggregate-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ function aggregateExceptionsFromError(
// 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) => {
const errors = error.errors;
const length = errors.length;
for (let i = 0; i < length && newExceptions.length < limit + 1; i++) {
const childError = errors[i];
if (isError(childError)) {
applyExceptionGroupFieldsForParentException(exception, exceptionId, error);
const newException = exceptionFromErrorImplementation(parser, childError);
Expand All @@ -92,15 +95,18 @@ function aggregateExceptionsFromError(
newExceptionId,
);
}
});
}
}

// SuppressedError (explicit resource management) wraps two errors: `error` (the new one thrown during disposal)
// and `suppressed` (the original one it replaced).
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SuppressedError

if (isSuppressedError(error)) {
['error', 'suppressed'].forEach(property => {
for (const property of ['error', 'suppressed']) {
if (newExceptions.length >= limit + 1) {
break;
}
const childError = error[property];
if (isError(childError)) {
applyExceptionGroupFieldsForParentException(exception, exceptionId, error);
Expand All @@ -118,7 +124,7 @@ function aggregateExceptionsFromError(
newExceptionId,
);
}
});
}
}

return newExceptions;
Expand Down
53 changes: 53 additions & 0 deletions packages/core/test/lib/utils/aggregate-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,44 @@ describe('applyAggregateErrorsToEvent()', () => {
});
});

test.each([
[0, ['Root Error']],
[1, ['First child', 'Root Error']],
[3, ['Third child', 'Second child', 'First child', 'Root Error']],
])('limits AggregateError children to %i linked errors', (limit, expectedValues) => {
const originalException: ExtendedError = runInNewContext(
`new AggregateError(['not an error', new Error('First child'), new Error('Second child'), new Error('Third child'), new Error('Fourth child')], 'Root Error')`,
);
const event = { exception: { values: [exceptionFromError(stackParser, originalException)] } };

applyAggregateErrorsToEvent(exceptionFromError, stackParser, 'cause', limit, event, { originalException });

expect(event.exception.values.map(exception => exception.value)).toEqual(expectedValues);
});

test('stops reading aggregate children when a nested cause uses the linked-error limit', () => {
const cause = new FakeAggregateError(
[new Error('First child'), new Error('Second child'), new Error('Third child')],
'Cause',
);
const originalException = new CustomAggregateError([new Error('Unread child')], 'Root Error', cause);
Object.defineProperty(originalException.errors, 0, {
get() {
throw new Error('Read an aggregate child after reaching the limit');
},
});
const event = { exception: { values: [exceptionFromError(stackParser, originalException)] } };

applyAggregateErrorsToEvent(exceptionFromError, stackParser, 'cause', 3, event, { originalException });

expect(event.exception.values.map(exception => exception.value)).toEqual([
'Second child',
'First child',
'Cause',
'Root Error',
]);
});

test('should keep the original mechanism type for the root exception', () => {
const fakeAggregateError = new FakeAggregateError(
[new Error('Nested Error 1'), new Error('Nested Error 2')],
Expand Down Expand Up @@ -541,6 +579,21 @@ describe('applyAggregateErrorsToEvent()', () => {
]);
});

test('limits nested SuppressedErrors before reading later siblings', () => {
const inner = new FakeSuppressedError(new Error('dispose failed'), new Error('Unread inner error'), 'Inner');
const originalException = new FakeSuppressedError(inner, new Error('Unread outer error'), 'Root Error');
Object.defineProperty(inner, 'suppressed', {
get() {
throw new Error('Read a suppressed error after reaching the limit');
},
});
const event = { exception: { values: [exceptionFromError(stackParser, originalException)] } };

applyAggregateErrorsToEvent(exceptionFromError, stackParser, 'cause', 2, event, { originalException });

expect(event.exception.values.map(exception => exception.value)).toEqual(['dispose failed', 'Inner', 'Root Error']);
});

test('skips `error` / `suppressed` values that are not Errors', () => {
const suppressedError = new FakeSuppressedError('just a string', new Error('Original error'));
const event: Event = { exception: { values: [exceptionFromError(stackParser, suppressedError)] } };
Expand Down
Loading