Skip to content

Commit

Permalink
Merge pull request #13405 from micalevisk/feat/issue-13400
Browse files Browse the repository at this point in the history
fix(core): auto flush logs on synchronous internal errors
  • Loading branch information
kamilmysliwiec authored Jun 3, 2024
2 parents 2e20dc4 + 47ca9d9 commit e93cf43
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/core/errors/exceptions-zone.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e93cf43

Please sign in to comment.