@@ -22,16 +22,15 @@ import {
2222 extractPathForTransaction ,
2323 extractRequestData ,
2424 isString ,
25- isThenable ,
2625 logger ,
27- normalize ,
2826} from '@sentry/utils' ;
2927
3028import type { NodeClient } from './client' ;
3129import { DEBUG_BUILD } from './debug-build' ;
3230// TODO (v8 / XXX) Remove this import
3331import type { ParseRequestOptions } from './requestDataDeprecated' ;
3432import { isAutoSessionTrackingEnabled } from './sdk' ;
33+ import { trpcMiddleware as newTrpcMiddleware } from './trpc' ;
3534
3635/**
3736 * Express-compatible tracing handler.
@@ -316,80 +315,25 @@ export function errorHandler(options?: {
316315 } ;
317316}
318317
319- interface SentryTrpcMiddlewareOptions {
320- /** Whether to include procedure inputs in reported events. Defaults to `false`. */
321- attachRpcInput ?: boolean ;
322- }
323-
324- interface TrpcMiddlewareArguments < T > {
325- path : string ;
326- type : string ;
327- next : ( ) => T ;
328- rawInput : unknown ;
329- }
330-
331318/**
332319 * Sentry tRPC middleware that names the handling transaction after the called procedure.
333320 *
334321 * Use the Sentry tRPC middleware in combination with the Sentry server integration,
335322 * e.g. Express Request Handlers or Next.js SDK.
323+ *
324+ * @deprecated Please use the top level export instead:
325+ * ```
326+ * // OLD
327+ * import * as Sentry from '@sentry/node';
328+ * Sentry.Handlers.trpcMiddleware();
329+ *
330+ * // NEW
331+ * import * as Sentry from '@sentry/node';
332+ * Sentry.trpcMiddleware();
333+ * ```
336334 */
337- export function trpcMiddleware ( options : SentryTrpcMiddlewareOptions = { } ) {
338- return function < T > ( { path, type, next, rawInput } : TrpcMiddlewareArguments < T > ) : T {
339- const clientOptions = getClient ( ) ?. getOptions ( ) ;
340- // eslint-disable-next-line deprecation/deprecation
341- const sentryTransaction = getCurrentScope ( ) . getTransaction ( ) ;
342-
343- if ( sentryTransaction ) {
344- sentryTransaction . updateName ( `trpc/${ path } ` ) ;
345- sentryTransaction . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_SOURCE , 'route' ) ;
346- sentryTransaction . op = 'rpc.server' ;
347-
348- const trpcContext : Record < string , unknown > = {
349- procedure_type : type ,
350- } ;
351-
352- if ( options . attachRpcInput !== undefined ? options . attachRpcInput : clientOptions ?. sendDefaultPii ) {
353- trpcContext . input = normalize ( rawInput ) ;
354- }
355-
356- // TODO: Can we rewrite this to an attribute? Or set this on the scope?
357- // eslint-disable-next-line deprecation/deprecation
358- sentryTransaction . setContext ( 'trpc' , trpcContext ) ;
359- }
360-
361- function captureIfError ( nextResult : { ok : false ; error ?: Error } | { ok : true } ) : void {
362- if ( ! nextResult . ok ) {
363- captureException ( nextResult . error , { mechanism : { handled : false , data : { function : 'trpcMiddleware' } } } ) ;
364- }
365- }
366-
367- let maybePromiseResult ;
368- try {
369- maybePromiseResult = next ( ) ;
370- } catch ( e ) {
371- captureException ( e , { mechanism : { handled : false , data : { function : 'trpcMiddleware' } } } ) ;
372- throw e ;
373- }
374-
375- if ( isThenable ( maybePromiseResult ) ) {
376- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
377- Promise . resolve ( maybePromiseResult ) . then (
378- nextResult => {
379- captureIfError ( nextResult as any ) ;
380- } ,
381- e => {
382- captureException ( e , { mechanism : { handled : false , data : { function : 'trpcMiddleware' } } } ) ;
383- } ,
384- ) ;
385- } else {
386- captureIfError ( maybePromiseResult as any ) ;
387- }
388-
389- // We return the original promise just to be safe.
390- return maybePromiseResult ;
391- } ;
392- }
335+ // eslint-disable-next-line deprecation/deprecation
336+ export const trpcMiddleware = newTrpcMiddleware ;
393337
394338// TODO (v8 / #5257): Remove this
395339// eslint-disable-next-line deprecation/deprecation
0 commit comments