From dd7b850dd4d525b3a15647c1dc24bb79a1079718 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Wed, 12 Aug 2026 22:39:32 -0700 Subject: [PATCH] fix: Handle non-JSON content type errors --- src/lib/error-interceptor.ts | 2 +- test/seam/connect/http-error.test.ts | 32 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/lib/error-interceptor.ts b/src/lib/error-interceptor.ts index 1f752b8d..ec297709 100644 --- a/src/lib/error-interceptor.ts +++ b/src/lib/error-interceptor.ts @@ -39,7 +39,7 @@ const isApiErrorResponse = ( if (headers == null) return false - const contentType = headers['Content-Type'] + const contentType = headers['content-type'] if ( typeof contentType === 'string' && !contentType.startsWith('application/json') diff --git a/test/seam/connect/http-error.test.ts b/test/seam/connect/http-error.test.ts index 27318f11..be435869 100644 --- a/test/seam/connect/http-error.test.ts +++ b/test/seam/connect/http-error.test.ts @@ -1,14 +1,44 @@ import test from 'ava' -import { AxiosError } from 'axios' +import { AxiosError, AxiosHeaders } from 'axios' import { getTestServer } from 'fixtures/seam/connect/api.js' import { + errorInterceptor, SeamHttp, SeamHttpApiError, SeamHttpInvalidInputError, SeamHttpUnauthorizedError, } from '@seamapi/http/connect' +test('SeamHttp: preserves AxiosError for JSON-shaped non-JSON response', async (t) => { + const data = { + error: { + type: 'bad_gateway', + message: 'A middlebox returned this error page', + }, + } + const err = new AxiosError( + 'Request failed with status code 502', + AxiosError.ERR_BAD_RESPONSE, + undefined, + undefined, + { + data, + status: 502, + statusText: 'Bad Gateway', + headers: new AxiosHeaders({ 'content-type': 'text/html; charset=utf-8' }), + config: { headers: new AxiosHeaders() }, + }, + ) + + const thrown = await t.throwsAsync(errorInterceptor(err), { + instanceOf: AxiosError, + }) + + t.is(thrown, err) + t.deepEqual(thrown.response?.data, data) +}) + test('SeamHttp: throws AxiosError on non-standard response', async (t) => { const { seed, endpoint } = await getTestServer(t)