ignore invalid json on error pages

This commit is contained in:
Bruno Windels 2020-10-05 17:18:05 +02:00
parent c8e4dbc1b3
commit 5ae1be9a9c
2 changed files with 10 additions and 2 deletions

View File

@ -85,7 +85,15 @@ export function createFetchRequest(createTimeout) {
}
const promise = fetch(url, options).then(async response => {
const {status} = response;
const body = await response.json();
let body;
try {
body = await response.json();
} catch (err) {
// some error pages return html instead of json, ignore error
if (!(err.name === "SyntaxError" && status >= 400)) {
throw err;
}
}
return {status, body};
}, err => {
if (err.name === "AbortError") {

View File

@ -66,7 +66,7 @@ export function xhrRequest(url, options) {
const xhr = send(url, options);
const promise = xhrAsPromise(xhr, options.method, url).then(xhr => {
const {status} = xhr;
let body = xhr.responseText;
let body = null;
if (xhr.getResponseHeader("Content-Type") === "application/json") {
body = JSON.parse(body);
}