From 5ae1be9a9c0a1c58dd4515b22d638448ac3fd27d Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 5 Oct 2020 17:18:05 +0200 Subject: [PATCH] ignore invalid json on error pages --- src/matrix/net/request/fetch.js | 10 +++++++++- src/matrix/net/request/xhr.js | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/matrix/net/request/fetch.js b/src/matrix/net/request/fetch.js index d1561692..eaa891ed 100644 --- a/src/matrix/net/request/fetch.js +++ b/src/matrix/net/request/fetch.js @@ -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") { diff --git a/src/matrix/net/request/xhr.js b/src/matrix/net/request/xhr.js index 3cde1b14..6685131e 100644 --- a/src/matrix/net/request/xhr.js +++ b/src/matrix/net/request/xhr.js @@ -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); }