Skip to content

Commit 870ccce

Browse files
authored
fix(handlers): prevent errors thrown on flush from breaking response
1 parent 7fe5717 commit 870ccce

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

packages/node/src/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,9 @@ export function requestHandler(
410410
.then(() => {
411411
_end.call(this, chunk, encoding, cb);
412412
})
413-
.then(null, e => {
413+
.catch(e => {
414414
logger.error(e);
415+
_end.call(this, chunk, encoding, cb);
415416
});
416417
};
417418
}

packages/node/test/handlers.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Hub } from '@sentry/hub';
33
import * as sentryHub from '@sentry/hub';
44
import { Transaction } from '@sentry/tracing';
55
import { Runtime } from '@sentry/types';
6+
import { SentryError } from '@sentry/utils';
67
import * as http from 'http';
78
import * as net from 'net';
89

@@ -16,6 +17,7 @@ import {
1617
requestHandler,
1718
tracingHandler,
1819
} from '../src/handlers';
20+
import * as SDK from '../src/sdk';
1921

2022
describe('parseRequest', () => {
2123
let mockReq: { [key: string]: any };
@@ -281,6 +283,31 @@ describe('requestHandler', () => {
281283
done();
282284
});
283285
});
286+
287+
it('patches `res.end` when `flushTimeout` is specified', () => {
288+
const flush = jest.spyOn(SDK, 'flush').mockResolvedValue(true);
289+
290+
const handler = requestHandler({ flushTimeout: 1337 });
291+
handler(req, res, next);
292+
res.end('ok');
293+
294+
setImmediate(() => {
295+
expect(flush).toBeCalledWith(1337);
296+
expect(res.finished).toBe(true);
297+
});
298+
});
299+
300+
it('prevents errors thrown during `flush` from breaking the response', async () => {
301+
jest.spyOn(SDK, 'flush').mockRejectedValue(new SentryError('HTTP Error (429)'));
302+
303+
const handler = requestHandler({ flushTimeout: 1337 });
304+
handler(req, res, next);
305+
res.end('ok');
306+
307+
setImmediate(() => {
308+
expect(res.finished).toBe(true);
309+
});
310+
});
284311
});
285312

286313
describe('tracingHandler', () => {

0 commit comments

Comments
 (0)