Skip to content

Commit

Permalink
Improve handling for non-json errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Jan 10, 2024
1 parent f4e613b commit 534ee7a
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,29 @@ class HttpClient {
if (!response.ok) {
throw response;
}

let responseBody = {};
try {
responseBody = await response.text();
responseBody = JSON.parse(responseBody);
} catch (error) {
/* */
}
return {
url,
headers: response.headers,
status: response.status,
data: await response.json().catch(e => e) || {}
data: responseBody
};
} catch (error) {
const resolvedError = typeof error.json === 'function' ? await error.json().catch(e => e) : error;
let resolvedError = error;
try {
resolvedError = await error.text();
resolvedError = JSON.parse(resolvedError);
} catch (parseError) {
/* */
}

const extensionErrorId = resolvedError.stack && resolvedError.stack.match(/chrome-extension:[/][/](\w+)[/]/);
if (extensionErrorId) {
this.logger && this.logger.debug && this.logger.debug({ title: `Fetch failed due to a browser extension - ${method} - ${url}`, method, url, data, headers, error, resolvedError, extensionErrorId });
Expand Down

0 comments on commit 534ee7a

Please sign in to comment.