Skip to content

Commit

Permalink
feat(logger): include enumerable properties in formatted errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi committed Oct 11, 2024
1 parent 7e07ce9 commit e8349ff
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
21 changes: 17 additions & 4 deletions packages/logger/src/formatter/LogFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,29 @@ 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 (
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

0 comments on commit e8349ff

Please sign in to comment.