mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 09:37:08 +00:00
Handle empty API responses
This commit is contained in:
@@ -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"]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user