Skip to content

Commit 232f240

Browse files
authored
fix: parse non-JSON response objects correctly (#56)
1 parent bcd3919 commit 232f240

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/BaseApi.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,20 @@ export class BaseApi {
5252
};
5353

5454
if (response.status < 200 || response.status >= 300) {
55-
const error = (await response.json()) as APIError;
55+
let error: APIError;
56+
try {
57+
error = (await response.json()) as APIError;
58+
} catch (_) {
59+
throw new StreamError(
60+
`Stream error: ${response.status} - ${response.statusText}`,
61+
metadata,
62+
response.status,
63+
);
64+
}
5665
throw new StreamError(
57-
`Stream error code ${error.code}: ${error.message}`,
66+
`Stream error code ${error!.code}: ${error!.message}`,
5867
metadata,
59-
error.code,
68+
error!.code,
6069
undefined,
6170
);
6271
}

0 commit comments

Comments
 (0)