From 2bc6ecbe4cb0f4d03d0a88968b5204092ea8a1d4 Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Wed, 15 Oct 2025 11:57:59 -0700 Subject: [PATCH] tRPC: improve Abort support --- src/server/trpc/trpc.router.fetchers.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/server/trpc/trpc.router.fetchers.ts b/src/server/trpc/trpc.router.fetchers.ts index 5a38f5590..f89b89c2b 100644 --- a/src/server/trpc/trpc.router.fetchers.ts +++ b/src/server/trpc/trpc.router.fetchers.ts @@ -82,13 +82,19 @@ async function _fetchFromTRPC signal, }; - // upstream fetch + // upstream FETCH response = await fetch(url, request); } catch (error: any) { - // NOTE: if signal?.aborted is true, we also come here, likely with a error?.name = ResponseAborted + // NOTE: if signal?.aborted is true, we also come here, likely with a error?.name = ResponseAborted (Next.js) or AbortError (standard from signal) // since we don't handle this case specially, the same TRPCError will be thrown as for other connection errors. + if (error?.name === 'AbortError') + throw new TRPCError({ + code: 'BAD_REQUEST', + message: (!throwWithoutName ? `[${moduleName} cancelled]: ` : '') + (error?.message || 'This operation was aborted.'), + cause: error, + }); // [logging - Connection error] candidate for the logging system const errorCause: object | undefined = error ? error?.cause ?? undefined : undefined; @@ -101,14 +107,10 @@ async function _fetchFromTRPC // Handle Connection errors - HTTP 400 throw new TRPCError({ code: 'BAD_REQUEST', - message: (throwWithoutName ? '' : `[${moduleName} network issue]: `) + message: (!throwWithoutName ? `[${moduleName} network issue]: ` : '') + (safeErrorString(error) || 'unknown fetch error') - + (errorCause - ? ` - ${safeErrorString(errorCause)}` - : '') - + ((errorCause && (errorCause as any)?.code === 'ECONNREFUSED') - ? ` - is "${url}" accessible by the server?` - : ''), + + (errorCause ? ` - ${safeErrorString(errorCause)}` : '') + + (errorCause && (errorCause as any)?.code === 'ECONNREFUSED' ? ` - is "${url}" accessible by the server?` : ''), cause: errorCause, }); }