@@ -16,6 +16,7 @@ import {
16
16
dropUndefinedKeys ,
17
17
extractPathForTransaction ,
18
18
isString ,
19
+ isThenable ,
19
20
logger ,
20
21
normalize ,
21
22
tracingContextFromHeaders ,
@@ -326,7 +327,7 @@ interface SentryTrpcMiddlewareOptions {
326
327
327
328
interface TrpcMiddlewareArguments < T > {
328
329
path : string ;
329
- type : 'query' | 'mutation' | 'subscription' ;
330
+ type : string ;
330
331
next : ( ) => T ;
331
332
rawInput : unknown ;
332
333
}
@@ -338,7 +339,7 @@ interface TrpcMiddlewareArguments<T> {
338
339
* e.g. Express Request Handlers or Next.js SDK.
339
340
*/
340
341
export function trpcMiddleware ( options : SentryTrpcMiddlewareOptions = { } ) {
341
- return async function < T > ( { path, type, next, rawInput } : TrpcMiddlewareArguments < T > ) : Promise < T > {
342
+ return function < T > ( { path, type, next, rawInput } : TrpcMiddlewareArguments < T > ) : T {
342
343
const hub = getCurrentHub ( ) ;
343
344
const clientOptions = hub . getClient ( ) ?. getOptions ( ) ;
344
345
const sentryTransaction = hub . getScope ( ) . getTransaction ( ) ;
@@ -358,36 +359,49 @@ export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
358
359
sentryTransaction . setContext ( 'trpc' , trpcContext ) ;
359
360
}
360
361
361
- function captureError ( e : unknown ) : void {
362
- captureException ( e , scope => {
363
- scope . addEventProcessor ( event => {
364
- addExceptionMechanism ( event , {
365
- handled : false ,
362
+ function shouldCaptureError ( e : unknown ) : boolean {
363
+ if ( typeof e === 'object' && e && 'code' in e ) {
364
+ // Is likely TRPCError - we only want to capture internal server errors
365
+ return e . code === 'INTERNAL_SERVER_ERROR' ;
366
+ } else {
367
+ // Is likely random error that bubbles up
368
+ return true ;
369
+ }
370
+ }
371
+
372
+ function handleErrorCase ( e : unknown ) : void {
373
+ if ( shouldCaptureError ( e ) ) {
374
+ captureException ( e , scope => {
375
+ scope . addEventProcessor ( event => {
376
+ addExceptionMechanism ( event , {
377
+ handled : false ,
378
+ } ) ;
379
+ return event ;
366
380
} ) ;
367
- return event ;
368
- } ) ;
369
381
370
- return scope ;
371
- } ) ;
382
+ return scope ;
383
+ } ) ;
384
+ }
372
385
}
373
386
374
- try {
375
- return await next ( ) ;
376
- } catch ( e : unknown ) {
377
- if ( typeof e === 'object' && e ) {
378
- if ( 'code' in e ) {
379
- // Is likely TRPCError - we only want to capture internal server errors
380
- if ( e . code === 'INTERNAL_SERVER_ERROR' ) {
381
- captureError ( e ) ;
382
- }
383
- } else {
384
- // Is likely random error that bubbles up
385
- captureError ( e ) ;
386
- }
387
- }
387
+ let maybePromiseResult ;
388
388
389
+ try {
390
+ maybePromiseResult = next ( ) ;
391
+ } catch ( e ) {
392
+ handleErrorCase ( e ) ;
389
393
throw e ;
390
394
}
395
+
396
+ if ( isThenable ( maybePromiseResult ) ) {
397
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
398
+ Promise . resolve ( maybePromiseResult ) . then ( null , e => {
399
+ handleErrorCase ( e ) ;
400
+ } ) ;
401
+ }
402
+
403
+ // We return the original promise just to be safe.
404
+ return maybePromiseResult ;
391
405
} ;
392
406
}
393
407
0 commit comments