11import { logger } from '@sentry/core' ;
2- import { getHubFromCarrier , Scope } from '@sentry/hub' ;
2+ import { getCurrentHub , Scope } from '@sentry/hub' ;
33import { SentryEvent , Severity } from '@sentry/types' ;
44import { forget } from '@sentry/utils/async' ;
55import { serialize } from '@sentry/utils/object' ;
@@ -9,7 +9,6 @@ import * as http from 'http';
99import * as os from 'os' ;
1010import * as url from 'url' ;
1111import { NodeClient } from './client' ;
12- import { getCurrentHub } from './hub' ;
1312
1413const DEFAULT_SHUTDOWN_TIMEOUT = 2000 ;
1514
@@ -208,20 +207,22 @@ export function requestHandler(options?: {
208207 transaction ?: boolean | TransactionTypes ;
209208 user ?: boolean ;
210209 version ?: boolean ;
211- } ) : ( req : http . IncomingMessage , res : http . ServerResponse , next : ( ) => void ) => void {
210+ } ) : ( req : http . IncomingMessage , res : http . ServerResponse , next : ( error ?: any ) => void ) => void {
212211 return function sentryRequestMiddleware (
213212 req : http . IncomingMessage ,
214- _res : http . ServerResponse ,
215- next : ( ) => void ,
213+ res : http . ServerResponse ,
214+ next : ( error ?: any ) => void ,
216215 ) : void {
217216 const local = domain . create ( ) ;
218- const hub = getHubFromCarrier ( req ) ;
219- hub . bindClient ( getCurrentHub ( ) . getClient ( ) ) ;
220- hub . configureScope ( ( scope : Scope ) => {
221- scope . addEventProcessor ( async ( event : SentryEvent ) => parseRequest ( event , req , options ) ) ;
222- } ) ;
217+ local . add ( req ) ;
218+ local . add ( res ) ;
223219 local . on ( 'error' , next ) ;
224- local . run ( next ) ;
220+ local . run ( ( ) => {
221+ getCurrentHub ( ) . configureScope ( scope =>
222+ scope . addEventProcessor ( async ( event : SentryEvent ) => parseRequest ( event , req , options ) ) ,
223+ ) ;
224+ next ( ) ;
225+ } ) ;
225226 } ;
226227}
227228
@@ -238,7 +239,6 @@ interface MiddlewareError extends Error {
238239/** JSDoc */
239240function getStatusCodeFromResponse ( error : MiddlewareError ) : number {
240241 const statusCode = error . status || error . statusCode || error . status_code || ( error . output && error . output . statusCode ) ;
241-
242242 return statusCode ? parseInt ( statusCode as string , 10 ) : 500 ;
243243}
244244
@@ -251,7 +251,7 @@ export function errorHandler(): (
251251) => void {
252252 return function sentryErrorMiddleware (
253253 error : MiddlewareError ,
254- req : http . IncomingMessage ,
254+ _req : http . IncomingMessage ,
255255 _res : http . ServerResponse ,
256256 next : ( error : MiddlewareError ) => void ,
257257 ) : void {
@@ -260,7 +260,7 @@ export function errorHandler(): (
260260 next ( error ) ;
261261 return ;
262262 }
263- getHubFromCarrier ( req ) . captureException ( error , { originalException : error } ) ;
263+ getCurrentHub ( ) . captureException ( error , { originalException : error } ) ;
264264 next ( error ) ;
265265 } ;
266266}
0 commit comments