|
| 1 | +import * as http from 'http'; |
| 2 | +import { getCurrentScope, getIsolationScope, setAsyncContextStrategy, setCurrentClient, withScope } from '@sentry/core'; |
| 3 | +import type { Scope } from '@sentry/types'; |
| 4 | +import { NodeClient } from '../../../src/sdk/client'; |
| 5 | +import { errorHandler } from '../../../src/sdk/handlers/errorHandler'; |
| 6 | +import { getDefaultNodeClientOptions } from '../../helpers/getDefaultNodePreviewClientOptions'; |
| 7 | + |
| 8 | +describe('errorHandler()', () => { |
| 9 | + beforeEach(() => { |
| 10 | + getCurrentScope().clear(); |
| 11 | + getIsolationScope().clear(); |
| 12 | + |
| 13 | + setAsyncContextStrategy(undefined); |
| 14 | + }); |
| 15 | + |
| 16 | + const headers = { ears: 'furry', nose: 'wet', tongue: 'spotted', cookie: 'favorite=zukes' }; |
| 17 | + const method = 'wagging'; |
| 18 | + const protocol = 'mutualsniffing'; |
| 19 | + const hostname = 'the.dog.park'; |
| 20 | + const path = '/by/the/trees/'; |
| 21 | + const queryString = 'chase=me&please=thankyou'; |
| 22 | + |
| 23 | + const sentryErrorMiddleware = errorHandler(); |
| 24 | + |
| 25 | + let req: http.IncomingMessage, res: http.ServerResponse, next: () => undefined; |
| 26 | + let client: NodeClient; |
| 27 | + |
| 28 | + function createNoOpSpy() { |
| 29 | + const noop = { noop: () => undefined }; // this is wrapped in an object so jest can spy on it |
| 30 | + return jest.spyOn(noop, 'noop') as any; |
| 31 | + } |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + req = { |
| 35 | + headers, |
| 36 | + method, |
| 37 | + protocol, |
| 38 | + hostname, |
| 39 | + originalUrl: `${path}?${queryString}`, |
| 40 | + } as unknown as http.IncomingMessage; |
| 41 | + res = new http.ServerResponse(req); |
| 42 | + next = createNoOpSpy(); |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + if (client['_sessionFlusher']) { |
| 47 | + clearInterval(client['_sessionFlusher']['_intervalId']); |
| 48 | + } |
| 49 | + jest.restoreAllMocks(); |
| 50 | + }); |
| 51 | + it('when autoSessionTracking is disabled, does not set requestSession status on Crash', done => { |
| 52 | + const options = getDefaultNodeClientOptions({ autoSessionTracking: false, release: '3.3' }); |
| 53 | + client = new NodeClient(options); |
| 54 | + // It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised |
| 55 | + // by the`requestHandler`) |
| 56 | + client.initSessionFlusher(); |
| 57 | + |
| 58 | + setCurrentClient(client); |
| 59 | + |
| 60 | + jest.spyOn<any, any>(client, '_captureRequestSession'); |
| 61 | + |
| 62 | + getIsolationScope().setRequestSession({ status: 'ok' }); |
| 63 | + |
| 64 | + let isolationScope: Scope; |
| 65 | + sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, () => { |
| 66 | + isolationScope = getIsolationScope(); |
| 67 | + return next(); |
| 68 | + }); |
| 69 | + |
| 70 | + setImmediate(() => { |
| 71 | + expect(isolationScope.getRequestSession()).toEqual({ status: 'ok' }); |
| 72 | + done(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash', done => { |
| 77 | + const options = getDefaultNodeClientOptions({ autoSessionTracking: false, release: '3.3' }); |
| 78 | + client = new NodeClient(options); |
| 79 | + setCurrentClient(client); |
| 80 | + |
| 81 | + jest.spyOn<any, any>(client, '_captureRequestSession'); |
| 82 | + |
| 83 | + getIsolationScope().setRequestSession({ status: 'ok' }); |
| 84 | + |
| 85 | + let isolationScope: Scope; |
| 86 | + sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, () => { |
| 87 | + isolationScope = getIsolationScope(); |
| 88 | + return next(); |
| 89 | + }); |
| 90 | + |
| 91 | + setImmediate(() => { |
| 92 | + expect(isolationScope.getRequestSession()).toEqual({ status: 'ok' }); |
| 93 | + done(); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('when autoSessionTracking is enabled, should set requestSession status to Crashed when an unhandled error occurs within the bounds of a request', () => { |
| 98 | + const options = getDefaultNodeClientOptions({ autoSessionTracking: true, release: '1.1' }); |
| 99 | + client = new NodeClient(options); |
| 100 | + // It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised |
| 101 | + // by the`requestHandler`) |
| 102 | + client.initSessionFlusher(); |
| 103 | + |
| 104 | + setCurrentClient(client); |
| 105 | + |
| 106 | + jest.spyOn<any, any>(client, '_captureRequestSession'); |
| 107 | + |
| 108 | + withScope(() => { |
| 109 | + getIsolationScope().setRequestSession({ status: 'ok' }); |
| 110 | + sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, () => { |
| 111 | + expect(getIsolationScope().getRequestSession()).toEqual({ status: 'crashed' }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('when autoSessionTracking is enabled, should not set requestSession status on Crash when it occurs outside the bounds of a request', done => { |
| 117 | + const options = getDefaultNodeClientOptions({ autoSessionTracking: true, release: '2.2' }); |
| 118 | + client = new NodeClient(options); |
| 119 | + // It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised |
| 120 | + // by the`requestHandler`) |
| 121 | + client.initSessionFlusher(); |
| 122 | + setCurrentClient(client); |
| 123 | + |
| 124 | + jest.spyOn<any, any>(client, '_captureRequestSession'); |
| 125 | + |
| 126 | + let isolationScope: Scope; |
| 127 | + sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, () => { |
| 128 | + isolationScope = getIsolationScope(); |
| 129 | + return next(); |
| 130 | + }); |
| 131 | + |
| 132 | + setImmediate(() => { |
| 133 | + expect(isolationScope.getRequestSession()).toEqual(undefined); |
| 134 | + done(); |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments