Skip to content

Commit

Permalink
fix(handlers): prevent errors thrown on flush from breaking response (
Browse files Browse the repository at this point in the history
#4667)

* fix(handlers): prevent errors thrown on `flush` from breaking response

Co-authored-by: Katie Byers <lobsterkatie@gmail.com>
Co-authored-by: Kamil Ogórek <kamil.ogorek@gmail.com>
  • Loading branch information
3 people committed Mar 3, 2022
1 parent f2b19f8 commit 0b2221d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export function requestHandler(
})
.then(null, e => {
logger.error(e);
_end.call(this, chunk, encoding, cb);
});
};
}
Expand Down
27 changes: 27 additions & 0 deletions packages/node/test/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Hub } from '@sentry/hub';
import * as sentryHub from '@sentry/hub';
import { Transaction } from '@sentry/tracing';
import { Runtime } from '@sentry/types';
import { SentryError } from '@sentry/utils';
import * as http from 'http';
import * as net from 'net';

Expand All @@ -16,6 +17,7 @@ import {
requestHandler,
tracingHandler,
} from '../src/handlers';
import * as SDK from '../src/sdk';

describe('parseRequest', () => {
let mockReq: { [key: string]: any };
Expand Down Expand Up @@ -281,6 +283,31 @@ describe('requestHandler', () => {
done();
});
});

it('patches `res.end` when `flushTimeout` is specified', () => {
const flush = jest.spyOn(SDK, 'flush').mockResolvedValue(true);

const sentryRequestMiddleware = requestHandler({ flushTimeout: 1337 });
sentryRequestMiddleware(req, res, next);
res.end('ok');

setImmediate(() => {
expect(flush).toHaveBeenCalledWith(1337);
expect(res.finished).toBe(true);
});
});

it('prevents errors thrown during `flush` from breaking the response', async () => {
jest.spyOn(SDK, 'flush').mockRejectedValue(new SentryError('HTTP Error (429)'));

const sentryRequestMiddleware = requestHandler({ flushTimeout: 1337 });
sentryRequestMiddleware(req, res, next);
res.end('ok');

setImmediate(() => {
expect(res.finished).toBe(true);
});
});
});

describe('tracingHandler', () => {
Expand Down

0 comments on commit 0b2221d

Please sign in to comment.