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

fix(core): auto flush logs on synchronous internal errors #13405

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/errors/exceptions-zone.ts
Expand Up @@ -9,7 +9,7 @@ export class ExceptionsZone {
public static run(
callback: () => void,
teardown: (err: any) => void = DEFAULT_TEARDOWN,
autoFlushLogs?: boolean,
autoFlushLogs: boolean,
) {
try {
callback();
Expand All @@ -25,7 +25,7 @@ export class ExceptionsZone {
public static async asyncRun(
callback: () => Promise<void>,
teardown: (err: any) => void = DEFAULT_TEARDOWN,
autoFlushLogs?: boolean,
autoFlushLogs: boolean,
) {
try {
await callback();
Expand Down
10 changes: 7 additions & 3 deletions packages/core/nest-factory.ts
Expand Up @@ -269,9 +269,13 @@ export class NestFactoryStatic {

return (...args: unknown[]) => {
let result: unknown;
ExceptionsZone.run(() => {
result = receiver[prop](...args);
}, teardown);
ExceptionsZone.run(
() => {
result = receiver[prop](...args);
},
teardown,
this.autoFlushLogs,
);

return result;
};
Expand Down
81 changes: 67 additions & 14 deletions packages/core/test/errors/test/exceptions-zone.spec.ts
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { Logger } from '@nestjs/common';
import { ExceptionsZone } from '../../../errors/exceptions-zone';

describe('ExceptionsZone', () => {
Expand All @@ -13,51 +14,103 @@ describe('ExceptionsZone', () => {
callback = sinon.spy();
});
it('should call callback', () => {
ExceptionsZone.run(callback as any, rethrow);
ExceptionsZone.run(callback as any, rethrow, false);
expect(callback.called).to.be.true;
});
describe('when callback throws exception', () => {
const exceptionHandler = {
handle: () => {},
};
let handleSpy: sinon.SinonSpy;
let LoggerFlushSpy: sinon.SinonSpy;
before(() => {
(ExceptionsZone as any).exceptionHandler = exceptionHandler;
handleSpy = sinon.spy(exceptionHandler, 'handle');
LoggerFlushSpy = sinon.spy(Logger, 'flush');
});
it('should call "handle" method of exceptionHandler and rethrows', () => {
const throwsCallback = () => {
throw new Error('');
};
expect(() => ExceptionsZone.run(throwsCallback, rethrow)).to.throws();
expect(handleSpy.called).to.be.true;
after(() => {
LoggerFlushSpy.restore();
});
describe('when callback throws exception and autoFlushLogs is false', () => {
it('should call "handle" method of exceptionHandler and rethrows and not flush logs', () => {
const throwsCallback = () => {
throw new Error('');
};
expect(() =>
ExceptionsZone.run(throwsCallback, rethrow, false),
).to.throws();

expect(handleSpy.called).to.be.true;

expect(LoggerFlushSpy.called).to.be.false;
});
});

describe('when callback throws exception and autoFlushLogs is true', () => {
it('should call "handle" method of exceptionHandler and rethrows and flush logs', () => {
const throwsCallback = () => {
throw new Error('');
};
expect(() =>
ExceptionsZone.run(throwsCallback, rethrow, true),
).to.throws();

expect(handleSpy.called).to.be.true;

expect(LoggerFlushSpy.called).to.be.true;
});
});
});
});

describe('asyncRun', () => {
let callback: sinon.SinonSpy;
beforeEach(() => {
callback = sinon.spy();
});
it('should call callback', async () => {
await ExceptionsZone.asyncRun(callback as any, rethrow);
await ExceptionsZone.asyncRun(callback as any, rethrow, false);
expect(callback.called).to.be.true;
});
describe('when callback throws exception', () => {
const exceptionHandler = {
handle: () => {},
};
let handleSpy: sinon.SinonSpy;
let LoggerFlushSpy: sinon.SinonSpy;
before(() => {
(ExceptionsZone as any).exceptionHandler = exceptionHandler;
handleSpy = sinon.spy(exceptionHandler, 'handle');
LoggerFlushSpy = sinon.spy(Logger, 'flush');
});
after(() => {
LoggerFlushSpy.restore();
});
it('should call "handle" method of exceptionHandler and rethrows error', async () => {
const throwsCallback = () => {
throw new Error('');
};
expect(ExceptionsZone.asyncRun(throwsCallback, rethrow)).to.eventually
.be.rejected;
describe('when callback throws exception and autoFlushLogs is false', () => {
it('should call "handle" method of exceptionHandler and rethrows error and not flush logs', async () => {
const throwsCallback = () => {
throw new Error('');
};
expect(ExceptionsZone.asyncRun(throwsCallback, rethrow, false)).to
.eventually.be.rejected;

expect(handleSpy.called).to.be.true;

expect(LoggerFlushSpy.called).to.be.false;
});
});
describe('when callback throws exception and autoFlushLogs is true', () => {
it('should call "handle" method of exceptionHandler and rethrows error and flush logs', async () => {
const throwsCallback = () => {
throw new Error('');
};
expect(ExceptionsZone.asyncRun(throwsCallback, rethrow, true)).to
.eventually.be.rejected;

expect(handleSpy.called).to.be.true;

expect(LoggerFlushSpy.called).to.be.true;
});
});
});
});
Expand Down