From 3485bb8d1b6becbc1c30afdcfc87a68a25fc0978 Mon Sep 17 00:00:00 2001 From: Shweelan Samson Date: Tue, 9 Jul 2019 16:24:07 -0400 Subject: [PATCH] Fix crash on non-JSON response in xhrRequest --- ui/store.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ui/store.ts b/ui/store.ts index 4b9ecf6..92bff74 100644 --- a/ui/store.ts +++ b/ui/store.ts @@ -119,12 +119,16 @@ export async function xhrRequest( let response: any; if (typeof deserialize === "function") { response = deserialize(xhr.responseText); - } else { + } else if ( + xhr.getResponseHeader("content-type").startsWith("application/json") + ) { try { response = xhr.responseText ? JSON.parse(xhr.responseText) : null; } catch (err) { - throw new Error("Invalid JSON: " + response); + throw new Error("Invalid JSON: " + xhr.responseText.slice(0, 80)); } + } else { + response = xhr.responseText; } // https://mithril.js.org/request.html#error-handling