From a4600a4d1d81de5370ad901e7d17a872433c5ba6 Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Wed, 15 Oct 2025 13:17:01 -0700 Subject: [PATCH] tRPC Fetchers: show content type on parse failures --- src/server/trpc/trpc.router.fetchers.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/server/trpc/trpc.router.fetchers.ts b/src/server/trpc/trpc.router.fetchers.ts index e4b990b17..908c4e02a 100644 --- a/src/server/trpc/trpc.router.fetchers.ts +++ b/src/server/trpc/trpc.router.fetchers.ts @@ -26,11 +26,14 @@ async function _jsonRequestParserOrThrow(response: Response) { // Errors: Cannot Parse if (error instanceof SyntaxError) { + const contentType = response.headers?.get('content-type')?.toLowerCase() || ''; + const contentTypeInfo = contentType && !contentType.includes('application/json') ? ` (Content-Type: ${contentType})` : ''; + // Improve messaging of Empty or Incomplete JSON if (error.message === 'Unexpected end of JSON input') throw new TRPCError({ code: 'PARSE_ERROR', - message: !text?.length ? 'Empty response while expecting JSON' : 'Incomplete JSON response', + message: (!text?.length ? 'Empty response while expecting JSON' : 'Incomplete JSON response') + contentTypeInfo, cause: error, }); @@ -50,14 +53,14 @@ async function _jsonRequestParserOrThrow(response: Response) { throw new TRPCError({ code: 'PARSE_ERROR', - message: `Expected JSON data but received ${inferredType ? inferredType + ', likely an error page' : 'NON-JSON content'}: \n\n"${text.length > 200 ? text.slice(0, 200) + '...' : text}"`, + message: `Expected JSON data but received ${inferredType ? inferredType + ', likely an error page' : 'NON-JSON content'}${contentTypeInfo}: \n\n"${text.length > 200 ? text.slice(0, 200) + '...' : text}"`, cause: error, }); } throw new TRPCError({ code: 'PARSE_ERROR', - message: `Error parsing JSON data: ${safeErrorString(error) || 'unknown error'}`, + message: `Error parsing JSON data${contentTypeInfo}: ${safeErrorString(error) || 'unknown error'}`, }); }