|
| 1 | +import { BacktraceRequestHandler } from '@backtrace/sdk-core'; |
| 2 | +import { BacktraceClient } from '../../src/index.js'; |
| 3 | +import { NodeOptionReader } from '../../src/common/NodeOptionReader.js'; |
| 4 | + |
| 5 | +describe('Unhandled error/rejection labeling', () => { |
| 6 | + let postedJson: string | undefined; |
| 7 | + let requestHandler: BacktraceRequestHandler; |
| 8 | + let client: BacktraceClient; |
| 9 | + |
| 10 | + const defaultClientOptions = { |
| 11 | + url: 'https://submit.backtrace.io/foo/bar/baz', |
| 12 | + metrics: { enable: false }, |
| 13 | + breadcrumbs: { enable: false }, |
| 14 | + }; |
| 15 | + |
| 16 | + const flushMicrotasks = () => new Promise((resolve) => setTimeout(resolve, 0)); |
| 17 | + |
| 18 | + const buildClient = () => { |
| 19 | + postedJson = undefined; |
| 20 | + requestHandler = { |
| 21 | + post: jest.fn().mockResolvedValue(Promise.resolve()), |
| 22 | + postError: jest.fn().mockImplementation((_url: string, json: string) => { |
| 23 | + postedJson = json; |
| 24 | + return Promise.resolve(); |
| 25 | + }), |
| 26 | + }; |
| 27 | + return BacktraceClient.builder(defaultClientOptions).useRequestHandler(requestHandler).build(); |
| 28 | + }; |
| 29 | + |
| 30 | + describe('uncaughtExceptionMonitor callback', () => { |
| 31 | + beforeEach(() => { |
| 32 | + client = buildClient(); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + client.dispose(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("Should tag origin 'unhandledRejection' as 'Unhandled rejection'", async () => { |
| 40 | + (process as unknown as { emit: (e: string, ...args: unknown[]) => void }).emit( |
| 41 | + 'uncaughtExceptionMonitor', |
| 42 | + new Error('rejected'), |
| 43 | + 'unhandledRejection', |
| 44 | + ); |
| 45 | + await flushMicrotasks(); |
| 46 | + |
| 47 | + expect(requestHandler.postError).toHaveBeenCalled(); |
| 48 | + const payload = JSON.parse(postedJson as string); |
| 49 | + expect(payload.attributes['error.type']).toBe('Unhandled rejection'); |
| 50 | + expect(payload.classifiers).toContain('UnhandledPromiseRejection'); |
| 51 | + }); |
| 52 | + |
| 53 | + it("Should tag origin 'uncaughtException' as 'Unhandled exception'", async () => { |
| 54 | + (process as unknown as { emit: (e: string, ...args: unknown[]) => void }).emit( |
| 55 | + 'uncaughtExceptionMonitor', |
| 56 | + new Error('boom'), |
| 57 | + 'uncaughtException', |
| 58 | + ); |
| 59 | + await flushMicrotasks(); |
| 60 | + |
| 61 | + expect(requestHandler.postError).toHaveBeenCalled(); |
| 62 | + const payload = JSON.parse(postedJson as string); |
| 63 | + expect(payload.attributes['error.type']).toBe('Unhandled exception'); |
| 64 | + expect(payload.classifiers ?? []).not.toContain('UnhandledPromiseRejection'); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("dedicated 'unhandledRejection' listener", () => { |
| 69 | + let nodeOptionReaderSpy: jest.SpyInstance; |
| 70 | + |
| 71 | + beforeEach(() => { |
| 72 | + // Force the dedicated unhandledRejection listener to be registered. |
| 73 | + // See BacktraceClient.captureUnhandledErrors: the listener is skipped |
| 74 | + // when running on Node 15+ with default --unhandled-rejections behavior. |
| 75 | + nodeOptionReaderSpy = jest.spyOn(NodeOptionReader, 'read').mockImplementation((flag: string) => { |
| 76 | + if (flag === 'unhandled-rejections') return 'warn'; |
| 77 | + return undefined; |
| 78 | + }); |
| 79 | + client = buildClient(); |
| 80 | + }); |
| 81 | + |
| 82 | + afterEach(() => { |
| 83 | + client.dispose(); |
| 84 | + nodeOptionReaderSpy.mockRestore(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("Should tag emitted 'unhandledRejection' events as 'Unhandled rejection'", async () => { |
| 88 | + (process as unknown as { emit: (e: string, ...args: unknown[]) => void }).emit( |
| 89 | + 'unhandledRejection', |
| 90 | + new Error('rejected'), |
| 91 | + Promise.resolve(), |
| 92 | + ); |
| 93 | + await flushMicrotasks(); |
| 94 | + |
| 95 | + expect(requestHandler.postError).toHaveBeenCalled(); |
| 96 | + const payload = JSON.parse(postedJson as string); |
| 97 | + expect(payload.attributes['error.type']).toBe('Unhandled rejection'); |
| 98 | + expect(payload.classifiers).toContain('UnhandledPromiseRejection'); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments