From c03bea1e01332f7cbd34dec96186ef50f50dfbf0 Mon Sep 17 00:00:00 2001 From: Ioan Moldovan Date: Mon, 30 Dec 2024 23:57:16 +0200 Subject: [PATCH] fix: failed to execute json on response error in console on message send --- extension/js/common/api/shared/api.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/extension/js/common/api/shared/api.ts b/extension/js/common/api/shared/api.ts index 324af840519..5137f2ee401 100644 --- a/extension/js/common/api/shared/api.ts +++ b/extension/js/common/api/shared/api.ts @@ -246,7 +246,14 @@ export class Api { } else if (resFmt === 'json') { try { const transformed = transformResponseWithProgressAndTimeout(); - return (await Promise.all([transformed.response.json(), transformed.pipe()]))[0] as FetchResult; + return ( + await Promise.all([ + transformed.response.text().then(text => { + return (text ? JSON.parse(text) : {}) as T; // Handle empty response body + }), + transformed.pipe(), + ]) + )[0] as FetchResult; } catch (e) { // handle empty response https://github.com/FlowCrypt/flowcrypt-browser/issues/5601 if (e instanceof SyntaxError && (e.message === 'Unexpected end of JSON input' || e.message.startsWith('JSON.parse: unexpected end of data'))) {