Skip to content

Commit

Permalink
fix: 先判断body类型
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Sep 19, 2024
1 parent cfe0bda commit 9bc3f78
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/modules/got-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> | undefined
if (Buffer.isBuffer(error.response.body)) {
body = JSON.parse(error.response.body.toString('utf-8')) as Record<string, unknown>
} else if (typeof error.response.body === 'object') {
body = error.response.body as Record<string, unknown>
} else if (typeof error.response.body === 'string') {
body = JSON.parse(error.response.body) as Record<string, unknown>
}
if (body && ServiceError.isServiceError(body)) {
throw ServiceError.fromJSON(body as unknown as Record<string, unknown>)
}
}
Expand Down

0 comments on commit 9bc3f78

Please sign in to comment.