Skip to content

Commit 61694a9

Browse files
committed
fix: properly parse API errors
1 parent 12644c8 commit 61694a9

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/utils/maa-copilot-client.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,22 @@ function createConfiguration(options?: ApiOptions) {
130130
throw new UnauthorizedError()
131131
}
132132

133-
if (response.status < 200 || response.status >= 300) {
134-
const body = await response.text()
133+
if (!response.ok) {
134+
let message: string | undefined
135135

136136
try {
137-
throw new ApiError(JSON.parse(body).message)
137+
const contentType = response.headers.get('content-type')
138+
139+
if (contentType?.includes('application/json')) {
140+
message = (await response.json()).message
141+
} else if (contentType?.includes('text/')) {
142+
message = await response.text()
143+
}
138144
} catch {
139-
throw new ApiError()
145+
// ignore
140146
}
147+
148+
throw new ApiError(message)
141149
}
142150

143151
;(response as ExtendedResponse).config = config

0 commit comments

Comments
 (0)