From 9bc3f781edef689725aedb54439885f20b845101 Mon Sep 17 00:00:00 2001 From: bangbang93 Date: Thu, 19 Sep 2024 09:26:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=88=E5=88=A4=E6=96=ADbody=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/got-hooks.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/modules/got-hooks.ts b/src/modules/got-hooks.ts index a6395b3..8d1abd8 100644 --- a/src/modules/got-hooks.ts +++ b/src/modules/got-hooks.ts @@ -6,8 +6,15 @@ export const beforeError: BeforeErrorHook[] = [catchServiceError] export function catchServiceError(error: RequestError): RequestError { if (error instanceof HTTPError) { if (error.response.headers['content-type']?.includes('application/json')) { - const body = JSON.parse(error.response.body.toString()) - if (ServiceError.isServiceError(body)) { + let body: Record | undefined + if (Buffer.isBuffer(error.response.body)) { + body = JSON.parse(error.response.body.toString('utf-8')) as Record + } else if (typeof error.response.body === 'object') { + body = error.response.body as Record + } else if (typeof error.response.body === 'string') { + body = JSON.parse(error.response.body) as Record + } + if (body && ServiceError.isServiceError(body)) { throw ServiceError.fromJSON(body as unknown as Record) } }