From e3680249a734b4c96b7388e2c558e996c2fa4460 Mon Sep 17 00:00:00 2001 From: Zita Szupera Date: Tue, 17 Sep 2024 10:38:29 +0200 Subject: [PATCH] fix: parse non-JSON response objects correctly --- src/BaseApi.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/BaseApi.ts b/src/BaseApi.ts index 6c51fb0..0260d3f 100644 --- a/src/BaseApi.ts +++ b/src/BaseApi.ts @@ -52,11 +52,21 @@ export class BaseApi { }; if (response.status < 200 || response.status >= 300) { - const error = (await response.json()) as APIError; + console.log(response); + let error: APIError; + try { + error = (await response.json()) as APIError; + } catch (_) { + throw new StreamError( + `Stream error: ${response.status} - ${response.statusText}`, + metadata, + response.status, + ); + } throw new StreamError( - `Stream error code ${error.code}: ${error.message}`, + `Stream error code ${error!.code}: ${error!.message}`, metadata, - error.code, + error!.code, undefined, ); }