From d33947e11a2e1d70c35736dd00d210a87c015f2b Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Sun, 16 Aug 2020 10:51:55 +0200 Subject: [PATCH] Handle empty API responses --- .../client/source/src/store/modules/api/actions.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/www/client/source/src/store/modules/api/actions.js b/lib/www/client/source/src/store/modules/api/actions.js index 93f761d..3722737 100644 --- a/lib/www/client/source/src/store/modules/api/actions.js +++ b/lib/www/client/source/src/store/modules/api/actions.js @@ -4,7 +4,18 @@ async function api ({state, commit, dispatch}, [resource, init = {}]) { // this.loading = true; const res = await fetch(`${state.apiUrl}${resource}`, init); if (res.ok) { - return await res.json(); + try { + return await res.json(); + } catch (err) { + if (err instanceof SyntaxError) { + if (Number(res.headers.get("Content-Length")) === 0) { + // res.json() cannot parse an empty response and we + // have run into this case. We just return undefined. + return; + } + } + throw err; + } } else { await dispatch('showSnack', [res.statusText, "warning"]); }