Fix crash on non-JSON response in xhrRequest

This commit is contained in:
Shweelan Samson authored and Zaid Abdulla committed 2019-07-13 01:42:33 -07:00
1 parent 5025564422
commit 3485bb8d1b
1 file changed
+6 -2
+6 -2
View File
@@ -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