Skip to content
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

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

Merged
merged 4 commits into from
Oct 15, 2024

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Oct 11, 2024

Summary

Changes

Please provide a summary of what's being changed

This PR improves the logic used to format errors for Logger to start including all enumerable properties of the error including both own and inherited.

This change was done in response to customer demand and considering it's additive, we don't consider it as a breaking change.

Consider the following error:

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';
  }
}

Before this change, calling logger.error('An error occurred', new SuperCustomError('foo', 303)); generated this log:

{
  "message": "foo",
  "location": "/path/to/my/source-code/my-service/handler.ts:18",
  "name": "SuperCustomError",
  "stack": "Error: Unexpected error #1    at lambdaHandler (/path/to/my/source-code/my-service/handler.ts:18:11) ..." 
}

This missed properties that customers might have wanted to see, making the troubleshooting harder than needed.

After the change, the log will instead look like this:

{
  "message": "foo",
  "location": "/path/to/my/source-code/my-service/handler.ts:18",
  "name": "SuperCustomError",
  "stack": "Error: Unexpected error #1    at lambdaHandler (/path/to/my/source-code/my-service/handler.ts:18:11) ...",
  "code": 303,
  "otherProperty": "otherProperty",
  "extraProperty": "extraProperty"
}

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: closes #2170


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@dreamorosi dreamorosi self-assigned this Oct 11, 2024
@dreamorosi dreamorosi requested a review from a team October 11, 2024 16:14
@dreamorosi dreamorosi requested a review from a team as a code owner October 11, 2024 16:14
@dreamorosi dreamorosi linked an issue Oct 11, 2024 that may be closed by this pull request
@boring-cyborg boring-cyborg bot added logger This item relates to the Logger Utility tests PRs that add or change tests labels Oct 11, 2024
@pull-request-size pull-request-size bot added the size/M PR between 30-99 LOC label Oct 11, 2024
@dreamorosi dreamorosi changed the title 2170 include error own properties in logged errors feat(logger): include enumerable properties in formatted errors Oct 11, 2024
@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label Oct 11, 2024
Copy link
Contributor

@am29d am29d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, only a small nitpick to reduce repetitive statements

Copy link

@dreamorosi dreamorosi requested a review from am29d October 15, 2024 09:04
@am29d am29d merged commit 4b80d9f into main Oct 15, 2024
21 checks passed
@am29d am29d deleted the 2170-include-error-own-properties-in-logged-errors branch October 15, 2024 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature PRs that introduce new features or minor changes logger This item relates to the Logger Utility size/M PR between 30-99 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: include error own properties in logged errors
2 participants