errors: trim error.cause.stack as well

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-12-05 12:22:36 +01:00
parent 57e0b112d6
commit 1d4b5b9d20
4 changed files with 27 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/errors': patch
---
Trim `error.cause.stack` in addition to `error.stack` when trimming stack traces from serialized errors.
@@ -63,6 +63,20 @@ describe('serialization', () => {
expect(withoutStack2.stack).not.toBeDefined();
});
it('serializes stack traces only when allowed with error cause', () => {
const before = new CustomError('m');
before.cause = new Error('cause');
const withStack: any = serializeError(before, { includeStack: true });
const withoutStack1: any = serializeError(before, { includeStack: false });
const withoutStack2: any = serializeError(before);
expect(withStack.stack).toEqual(before.stack);
expect(withStack.cause.stack).toEqual(withStack.cause.stack);
expect(withoutStack1.stack).not.toBeDefined();
expect(withoutStack2.stack).not.toBeDefined();
expect(withoutStack1.cause.stack).not.toBeDefined();
expect(withoutStack2.cause.stack).not.toBeDefined();
});
it('stringifies all supported forms', () => {
expect(stringifyError({})).toEqual("unknown error '[object Object]'");
expect(
@@ -60,6 +60,14 @@ export function serializeError(
if (!options?.includeStack) {
delete result.stack;
if (
result.cause &&
typeof result.cause === 'object' &&
'stack' in result.cause
) {
delete result.cause.stack;
}
}
return result;
@@ -442,7 +442,6 @@ describe('Catalog Backend Integration', () => {
cause: {
name: 'Error',
message: 'NOPE',
stack: expect.stringMatching(/^Error: NOPE/),
},
},
},