Skip to content

feat(logger): include enumerable properties in formatted errors #3195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 18 additions & 4 deletions packages/logger/src/formatter/LogFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,30 @@ abstract class LogFormatter {
* @param error - Error to format
*/
public formatError(error: Error): LogAttributes {
return {
name: error.name,
const { name, message, stack, cause, ...errorAttributes } = error;
const formattedError: LogAttributes = {
name,
location: this.getCodeLocation(error.stack),
message: error.message,
stack: error.stack,
message,
stack,
cause:
error.cause instanceof Error
? this.formatError(error.cause)
: error.cause,
};
for (const key in error) {
if (
typeof key === 'string' &&
key !== 'name' &&
key !== 'message' &&
key !== 'stack' &&
key !== 'cause'
) {
formattedError[key] = (errorAttributes as Record<string, unknown>)[key];
}
}

return formattedError;
}

/**
Expand Down
63 changes: 58 additions & 5 deletions packages/logger/tests/unit/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ describe('Formatters', () => {
},
{
error: new AssertionError({
message: 'Expected values to be strictly equal',
actual: 1,
expected: 2,
operator: 'strictEqual',
Expand All @@ -369,6 +368,11 @@ describe('Formatters', () => {
),
message: expect.stringMatching(/Expected values to be strictly equal/),
cause: undefined,
actual: 1,
expected: 2,
operator: 'strictEqual',
code: 'ERR_ASSERTION',
generatedMessage: true,
},
},
{
Expand Down Expand Up @@ -432,16 +436,65 @@ describe('Formatters', () => {
cause: 'bar',
},
},
])('formats errors correctly ($name)', ({ error, name, expectedFields }) => {
])(
'formats standard errors correctly ($name)',
({ error, name, expectedFields }) => {
// Act
const formattedError = formatter.formatError(error);

// Assess
expect(formattedError).toEqual({
location: expect.stringMatching(fileNameRegexp),
stack: expect.stringMatching(fileNameRegexpWithLine),
name,
...expectedFields,
});
}
);

it('formats custom errors by including only enumerable properties', () => {
// Prepare
const customSymbol = Symbol('customSymbol');
class CustomError extends Error {
public otherProperty: string;

public constructor(
message: string,
public readonly code: number
) {
super(message);
this.name = 'CustomError';
this.otherProperty = 'otherProperty';
}

public [customSymbol] = (): void => {
// do nothing
};
}

class SuperCustomError extends CustomError {
public extraProperty: string;
public constructor(message: string, code: number) {
super(message, code);
this.name = 'SuperCustomError';
this.extraProperty = 'extraProperty';
}
}

// Act
const formattedError = formatter.formatError(error);
const formattedError = formatter.formatError(
new SuperCustomError('Something went wrong', 500)
);

// Assess
expect(formattedError).toEqual({
location: expect.stringMatching(fileNameRegexp),
stack: expect.stringMatching(fileNameRegexpWithLine),
name,
...expectedFields,
name: 'SuperCustomError',
message: 'Something went wrong',
code: 500,
otherProperty: 'otherProperty',
extraProperty: 'extraProperty',
});
});

Expand Down