From 2780eeb4eac8e3fc4439b8b8a20d65396c996b05 Mon Sep 17 00:00:00 2001 From: Robbe Van Petegem Date: Mon, 25 Jan 2021 19:00:56 +0100 Subject: [PATCH] Catch errors in response.json() --- src/api/fetch.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/fetch.js b/src/api/fetch.js index 2073afdb..368c89ee 100644 --- a/src/api/fetch.js +++ b/src/api/fetch.js @@ -9,7 +9,7 @@ const fetchRetry = require("fetch-retry")(fetch, { export async function* indexGenerator(path, auth) { let page = 1; while (true) { - let response; + let response, result; try { response = await fetchRetry(`${baseURL}/${path}?page=${page}`, { retries: 5, @@ -19,10 +19,10 @@ export async function* indexGenerator(path, auth) { "x-device-id": auth.device_id, }, }); + result = await response.json(); } catch (reason) { throw { error: [reason] }; } - const result = await response.json(); if (response.ok && result) { const loaded = new Date(); for (let obj in result) { @@ -41,13 +41,13 @@ export async function* indexGenerator(path, auth) { } async function resolveRequest(path, options) { - let response; + let response, result; try { response = await fetchRetry(`${baseURL}/${path}`, options); + result = response.status === 204 ? true : await response.json(); } catch (reason) { throw { error: [reason] }; } - const result = response.status === 204 ? true : await response.json(); if (response.ok) { return result; } else {