Skip to content

Commit 6a07a74

Browse files
authored
nestjs: fix skipIfClientUndefined not working in BacktraceModule (#309)
Co-authored-by: Sebastian Alex <sebastian.alex@saucelabs.com>
1 parent 7096a9f commit 6a07a74

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

packages/nestjs/src/backtrace.module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new ConfigurableModule
4040
(instanceOrOptions instanceof BacktraceClient ? instanceOrOptions : instanceOrOptions?.client) ??
4141
BacktraceClient.instance;
4242

43-
if (!instance) {
43+
const skipIfClientUndefined =
44+
instanceOrOptions instanceof BacktraceClient
45+
? false
46+
: instanceOrOptions?.options?.skipIfClientUndefined;
47+
48+
if (!instance && !skipIfClientUndefined) {
4449
throw new Error(
4550
'Backtrace instance is not available. Initialize it first, or pass it into the module using register/registerAsync.',
4651
);

packages/nestjs/tests/backtrace.handler.spec.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ describe('BacktraceInterceptor', () => {
3939

4040
class Filter extends BaseExceptionFilter {
4141
catch(exception: unknown, host: ArgumentsHost): void {
42-
handler.handleException(exception, host);
42+
try {
43+
handler.handleException(exception, host);
44+
} catch (err) {
45+
// Do nothing
46+
}
4347
super.catch(exception, host);
4448
}
4549
}
@@ -122,6 +126,57 @@ describe('BacktraceInterceptor', () => {
122126
expect(send).not.toBeCalled();
123127
});
124128

129+
it('should throw if client is uninitialized', async () => {
130+
const error = new Error('foo');
131+
132+
@Controller()
133+
class TestController {
134+
@Get('error')
135+
public error() {
136+
throw error;
137+
}
138+
}
139+
140+
const interceptor = new BacktraceExceptionHandler({});
141+
const handleException = jest.spyOn(interceptor, 'handleException');
142+
143+
const { app } = await createAppWithHandler(interceptor, TestController);
144+
145+
await app.init();
146+
await request(app.getHttpServer()).get('/error').expect(500);
147+
148+
const result = handleException.mock.results[0];
149+
expect(result.type).toEqual('throw');
150+
expect(result.value).toBeInstanceOf(Error);
151+
expect(result.value.message).toBe('Backtrace instance is unavailable. Initialize the client first.');
152+
});
153+
154+
it('should not throw if client is uninitialized and skipIfClientUndefined is true', async () => {
155+
const error = new Error('foo');
156+
157+
@Controller()
158+
class TestController {
159+
@Get('error')
160+
public error() {
161+
throw error;
162+
}
163+
}
164+
165+
const interceptor = new BacktraceExceptionHandler({
166+
skipIfClientUndefined: true,
167+
});
168+
const handleException = jest.spyOn(interceptor, 'handleException');
169+
170+
const { app } = await createAppWithHandler(interceptor, TestController);
171+
172+
await app.init();
173+
await request(app.getHttpServer()).get('/error').expect(500);
174+
175+
const result = handleException.mock.results[0];
176+
expect(result.type).not.toEqual('throw');
177+
expect(result.value).toBe(false);
178+
});
179+
125180
describe('include', () => {
126181
it('should not send when error type is not on include list', async () => {
127182
const error = new BadRequestException('abc');

packages/nestjs/tests/backtrace.module.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,18 @@ describe('BacktraceModule', () => {
5656
}).compile(),
5757
).rejects.toThrowError(/Backtrace instance is not available\./);
5858
});
59+
60+
it('should not throw an error when instance is not initialized and skipIfClientUndefined is true', async () => {
61+
await expect(
62+
Test.createTestingModule({
63+
imports: [
64+
BacktraceModule.register({
65+
options: {
66+
skipIfClientUndefined: true,
67+
},
68+
}),
69+
],
70+
}).compile(),
71+
).resolves.not.toThrow();
72+
});
5973
});

0 commit comments

Comments
 (0)