Skip to content
Merged
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
7 changes: 6 additions & 1 deletion packages/nestjs/src/backtrace.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new ConfigurableModule
(instanceOrOptions instanceof BacktraceClient ? instanceOrOptions : instanceOrOptions?.client) ??
BacktraceClient.instance;

if (!instance) {
const skipIfClientUndefined =
instanceOrOptions instanceof BacktraceClient
? false
: instanceOrOptions?.options?.skipIfClientUndefined;

if (!instance && !skipIfClientUndefined) {
throw new Error(
'Backtrace instance is not available. Initialize it first, or pass it into the module using register/registerAsync.',
);
Expand Down
57 changes: 56 additions & 1 deletion packages/nestjs/tests/backtrace.handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ describe('BacktraceInterceptor', () => {

class Filter extends BaseExceptionFilter {
catch(exception: unknown, host: ArgumentsHost): void {
handler.handleException(exception, host);
try {
handler.handleException(exception, host);
} catch (err) {
// Do nothing
}
super.catch(exception, host);
}
}
Expand Down Expand Up @@ -122,6 +126,57 @@ describe('BacktraceInterceptor', () => {
expect(send).not.toBeCalled();
});

it('should throw if client is uninitialized', async () => {
const error = new Error('foo');

@Controller()
class TestController {
@Get('error')
public error() {
throw error;
}
}

const interceptor = new BacktraceExceptionHandler({});
const handleException = jest.spyOn(interceptor, 'handleException');

const { app } = await createAppWithHandler(interceptor, TestController);

await app.init();
await request(app.getHttpServer()).get('/error').expect(500);

const result = handleException.mock.results[0];
expect(result.type).toEqual('throw');
expect(result.value).toBeInstanceOf(Error);
expect(result.value.message).toBe('Backtrace instance is unavailable. Initialize the client first.');
});

it('should not throw if client is uninitialized and skipIfClientUndefined is true', async () => {
const error = new Error('foo');

@Controller()
class TestController {
@Get('error')
public error() {
throw error;
}
}

const interceptor = new BacktraceExceptionHandler({
skipIfClientUndefined: true,
});
const handleException = jest.spyOn(interceptor, 'handleException');

const { app } = await createAppWithHandler(interceptor, TestController);

await app.init();
await request(app.getHttpServer()).get('/error').expect(500);

const result = handleException.mock.results[0];
expect(result.type).not.toEqual('throw');
expect(result.value).toBe(false);
});

describe('include', () => {
it('should not send when error type is not on include list', async () => {
const error = new BadRequestException('abc');
Expand Down
14 changes: 14 additions & 0 deletions packages/nestjs/tests/backtrace.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,18 @@ describe('BacktraceModule', () => {
}).compile(),
).rejects.toThrowError(/Backtrace instance is not available\./);
});

it('should not throw an error when instance is not initialized and skipIfClientUndefined is true', async () => {
await expect(
Test.createTestingModule({
imports: [
BacktraceModule.register({
options: {
skipIfClientUndefined: true,
},
}),
],
}).compile(),
).resolves.not.toThrow();
});
});