tRPC Fetchers: show content type on parse failures

This commit is contained in:
Enrico Ros
2025-10-15 13:17:01 -07:00
parent ad6a465ce7
commit a4600a4d1d
+6 -3
View File
@@ -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'}`,
});
}