@@ -351,36 +351,32 @@ export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
351
351
sentryTransaction . setContext ( 'trpc' , trpcContext ) ;
352
352
}
353
353
354
- function shouldCaptureError ( e : unknown ) : boolean {
355
- if ( typeof e === 'object' && e && 'code' in e ) {
356
- // Is likely TRPCError - we only want to capture internal server errors
357
- return e . code === 'INTERNAL_SERVER_ERROR' ;
358
- } else {
359
- // Is likely random error that bubbles up
360
- return true ;
361
- }
362
- }
363
-
364
- function handleErrorCase ( e : unknown ) : void {
365
- if ( shouldCaptureError ( e ) ) {
366
- captureException ( e , { mechanism : { handled : false } } ) ;
354
+ function captureIfError ( nextResult : { ok : false ; error ?: Error } | { ok : true } ) : void {
355
+ if ( ! nextResult . ok ) {
356
+ captureException ( nextResult . error , { mechanism : { handled : false , data : { function : 'trpcMiddleware' } } } ) ;
367
357
}
368
358
}
369
359
370
360
let maybePromiseResult ;
371
-
372
361
try {
373
362
maybePromiseResult = next ( ) ;
374
363
} catch ( e ) {
375
- handleErrorCase ( e ) ;
364
+ captureException ( e , { mechanism : { handled : false , data : { function : 'trpcMiddleware' } } } ) ;
376
365
throw e ;
377
366
}
378
367
379
368
if ( isThenable ( maybePromiseResult ) ) {
380
369
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
381
- Promise . resolve ( maybePromiseResult ) . then ( null , e => {
382
- handleErrorCase ( e ) ;
383
- } ) ;
370
+ Promise . resolve ( maybePromiseResult ) . then (
371
+ nextResult => {
372
+ captureIfError ( nextResult as any ) ;
373
+ } ,
374
+ e => {
375
+ captureException ( e , { mechanism : { handled : false , data : { function : 'trpcMiddleware' } } } ) ;
376
+ } ,
377
+ ) ;
378
+ } else {
379
+ captureIfError ( maybePromiseResult as any ) ;
384
380
}
385
381
386
382
// We return the original promise just to be safe.
0 commit comments