Skip to content

feat(logger): add cause field to formatted error #1617

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 1 commit into from
Jul 17, 2023
Merged
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
22 changes: 22 additions & 0 deletions packages/logger/src/formatter/LogFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { LogFormatterInterface } from '.';
import { LogAttributes, UnformattedAttributes } from '../types';

/**
* Typeguard to monkey patch Error to add a cause property.
*
* This is needed because the `cause` property was added in Node 16.x.
* Since we want to be able to format errors in Node 14.x, we need to
* add this property ourselves. We can remove this once we drop support
* for Node 14.x.
*
* @see 1361
* @see https://nodejs.org/api/errors.html#errors_error_cause
*/
const isErrorWithCause = (
error: Error
): error is Error & { cause: unknown } => {
return 'cause' in error;
};

/**
* This class defines and implements common methods for the formatting of log attributes.
*
Expand Down Expand Up @@ -31,6 +48,11 @@ abstract class LogFormatter implements LogFormatterInterface {
location: this.getCodeLocation(error.stack),
message: error.message,
stack: error.stack,
cause: isErrorWithCause(error)
? error.cause instanceof Error
? this.formatError(error.cause)
: error.cause
: undefined,
};
}

Expand Down
65 changes: 65 additions & 0 deletions packages/logger/tests/unit/formatter/PowertoolLogFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,71 @@ describe('Class: PowertoolLogFormatter', () => {

expect(shouldThrow).toThrowError(expect.any(URIError));
});

test('when an error with cause of type Error is formatted, the cause key is included and formatted', () => {
// Prepare
const formatter = new PowertoolLogFormatter();
class ErrorWithCause extends Error {
public cause?: Error;
public constructor(message: string, options?: { cause: Error }) {
super(message);
this.cause = options?.cause;
}
}

// Act
const formattedURIError = formatter.formatError(
new ErrorWithCause('foo', { cause: new Error('bar') })
);

// Assess
expect(formattedURIError).toEqual({
location: expect.stringMatching(/PowertoolLogFormatter.test.ts:[0-9]+/),
message: 'foo',
name: 'Error',
stack: expect.stringMatching(
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
),
cause: {
location: expect.stringMatching(
/PowertoolLogFormatter.test.ts:[0-9]+/
),
message: 'bar',
name: 'Error',
stack: expect.stringMatching(
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
),
},
});
});

test('when an error with cause of type other than Error is formatted, the cause key is included as-is', () => {
// Prepare
const formatter = new PowertoolLogFormatter();
class ErrorWithCause extends Error {
public cause?: unknown;
public constructor(message: string, options?: { cause: unknown }) {
super(message);
this.cause = options?.cause;
}
}

// Act
const formattedURIError = formatter.formatError(
new ErrorWithCause('foo', { cause: 'bar' })
);

// Assess
expect(formattedURIError).toEqual({
location: expect.stringMatching(/PowertoolLogFormatter.test.ts:[0-9]+/),
message: 'foo',
name: 'Error',
stack: expect.stringMatching(
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
),
cause: 'bar',
});
});
});

describe('Method: formatTimestamp', () => {
Expand Down