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(core): Modify BaseExceptionFilter to include error properties in error logging #13681

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jochongs
Copy link

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #13550

The class is defined as follows. This class includes the properties detail and code.

export class MyCustomError extends Error {
  detail: any;
  code: string;

  constructor(message: string, detail: string, code: string) {
    super(message);
    this.detail = detail;
    this.code = code;
  }
}

If thrown like this, the detail of the error and the information about 42883 are not logged by the BaseExceptionFilter.

@Get('/')
async helloWorld() {
  throw new MyCustomError('My custom error', 'Detail Error', '42883');
}

As shown in the image below:
image

So, I created a protected method that logs the Error object to the console.

protected printError(error: Error) {
  if (!error) {
    return;
  }
  console.error(error);
}

What is the new behavior?

Now, all the properties of the Error object processed by the BaseExceptionFilter can be seen in the logs.

image

Does this PR introduce a breaking change?

  • Yes
  • No

The error method of the Logger class has been modified to log more than just the stack trace as a string when an Error object is passed. I'm not sure if this qualifies as a breaking change.

Other information

I'm not sure if there's a specific reason why the console.error method shouldn't be used.

If it cannot be used, you can modify the handleUnknownError method in the BaseExceptionFilter as follows to check the properties of the error:

if (this.isExceptionObject(exception)) {
  return BaseExceptionFilter.logger.error(
    exception.message,
    exception.stack + ' ' + JSON.stringify(exception, null, 2),
  );
}

However, this approach is not exactly the same as using console.error; it does not provide color coding or emphasis on the stack trace, as shown in the image below.

image

If using the console.error method is not an option, we may need to manually create a string representation of the object to display as shown above.

@coveralls
Copy link

coveralls commented Jun 13, 2024

Pull Request Test Coverage Report for Build 5174189c-b106-4554-ab6a-9dd2f74f8778

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.02%) to 92.191%

Files with Coverage Reduction New Missed Lines %
packages/core/exceptions/base-exception-filter.ts 1 88.0%
Totals Coverage Status
Change from base Build e18002f5-8ff6-4564-ba7e-328984ab8501: -0.02%
Covered Lines: 6741
Relevant Lines: 7312

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants