From aac37fd9af4cbb670a50786574e3b85d515bea00 Mon Sep 17 00:00:00 2001 From: Danijel Martinek Date: Wed, 6 May 2026 11:54:51 +0200 Subject: [PATCH] docs(core-shared): correct defineErrorMiddleware jsdoc to match implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Code-quality reviewer flagged a stale 'try/catch' phrase in the jsdoc that didn't match the actual result.ok inspection pattern. Updated description to accurately describe how tRPC v11 middleware sees errors (returned result, not thrown exception) and how the middleware discriminates and rethrows. Pure docstring change — no behavioral or signature impact. --- .../src/trpc/define-error-middleware.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/core-shared/src/trpc/define-error-middleware.ts b/packages/core-shared/src/trpc/define-error-middleware.ts index a9ef641..681b596 100644 --- a/packages/core-shared/src/trpc/define-error-middleware.ts +++ b/packages/core-shared/src/trpc/define-error-middleware.ts @@ -7,11 +7,16 @@ type ErrorCtor = new (...args: never[]) => Error; /** * Build a tRPC middleware that translates domain errors to TRPCError. * - * Each tuple pairs a constructor with a TRPC error code. The middleware - * runs the procedure body inside a try/catch; on `instanceof Ctor`, - * it throws a TRPCError with the configured code and the original error - * preserved as `.cause`. Unmapped errors propagate untouched (tRPC's - * default INTERNAL_SERVER_ERROR handling applies). + * Each tuple pairs a constructor with a TRPC error code. tRPC v11 + * middleware sees procedure throws as a returned `result` (not an + * exception): `next()` resolves to `{ ok: false, error: TRPCError }` + * where `error.cause` is the original domain error wrapped by + * `getTRPCErrorFromUnknown`. The middleware checks `result.ok`, + * matches `result.error.cause` against each `instanceof Ctor`, and + * on a hit throws a fresh TRPCError with the configured code and the + * original error preserved as `.cause`. Unmapped errors fall through + * unchanged — tRPC's default INTERNAL_SERVER_ERROR wrapping applies + * with the original error still reachable via `.cause`. * * Owned by features: each feature passes its own constructors in. * core-shared never enumerates feature-specific error classes.