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
Open
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
13 changes: 13 additions & 0 deletions packages/common/services/console-logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export class ConsoleLogger implements LoggerService {
const { messages, context, stack } =
this.getContextAndStackAndMessagesToPrint([message, ...optionalParams]);

if (message instanceof Error) {
this.printMessages(messages, context, 'error', 'stderr');
this.printError(message);
return;
}

this.printMessages(messages, context, 'error', 'stderr');
this.printStackTrace(stack);
}
Expand Down Expand Up @@ -278,6 +284,13 @@ export class ConsoleLogger implements LoggerService {
process.stderr.write(`${stack}\n`);
}

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

protected updateAndGetTimestampDiff(): string {
const includeTimestamp =
ConsoleLogger.lastTimestampAt && this.options?.timestamp;
Expand Down
5 changes: 5 additions & 0 deletions packages/common/services/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export class Logger implements LoggerService {
error(message: any, ...optionalParams: [...any, string?, string?]): void;
@Logger.WrapBuffer
error(message: any, ...optionalParams: any[]) {
if (message instanceof Error) {
this.localInstance.error(message);
return;
}

optionalParams = this.context
? (optionalParams.length ? optionalParams : [undefined]).concat(
this.context,
Expand Down
9 changes: 5 additions & 4 deletions packages/common/test/services/logger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ describe('Logger', () => {
describe('when the default logger is used', () => {
let processStdoutWriteSpy: sinon.SinonSpy;
let processStderrWriteSpy: sinon.SinonSpy;
let consoleErrorSpy: sinon.SinonSpy;

beforeEach(() => {
processStdoutWriteSpy = sinon.spy(process.stdout, 'write');
processStderrWriteSpy = sinon.spy(process.stderr, 'write');
consoleErrorSpy = sinon.spy(console, 'error');
});

afterEach(() => {
processStdoutWriteSpy.restore();
processStderrWriteSpy.restore();
consoleErrorSpy.restore();
});

it('should print one message to the console', () => {
Expand Down Expand Up @@ -111,10 +114,8 @@ describe('Logger', () => {

Logger.error(error);

expect(processStderrWriteSpy.calledOnce).to.be.true;
expect(processStderrWriteSpy.firstCall.firstArg).to.include(
`Error: Random text here`,
);
expect(consoleErrorSpy.calledOnce).to.be.true;
expect(consoleErrorSpy.firstCall.firstArg).to.equal(error);
});

it('should serialise a plain JS object (as a message) without context to the console', () => {
Expand Down
6 changes: 0 additions & 6 deletions packages/core/exceptions/base-exception-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ export class BaseExceptionFilter<T = any> implements ExceptionFilter<T> {
applicationRef.end(response);
}

if (this.isExceptionObject(exception)) {
return BaseExceptionFilter.logger.error(
exception.message,
exception.stack,
);
}
return BaseExceptionFilter.logger.error(exception);
}

Expand Down