From 85bfef01602ce695f0e9e858eb1ff3600733637a 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 | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/BaseApi.ts b/src/BaseApi.ts index 6c51fb0..b429674 100644 --- a/src/BaseApi.ts +++ b/src/BaseApi.ts @@ -52,11 +52,20 @@ export class BaseApi { }; if (response.status < 200 || response.status >= 300) { - const error = (await response.json()) as APIError; + 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, ); }