Fix crash on non-JSON response in xhrRequest
This commit is contained in:
1 parent
5025564422
commit
3485bb8d1b
1 file changed
+6
-2
+6
-2
@@ -119,12 +119,16 @@ export async function xhrRequest(
|
|||||||
let response: any;
|
let response: any;
|
||||||
if (typeof deserialize === "function") {
|
if (typeof deserialize === "function") {
|
||||||
response = deserialize(xhr.responseText);
|
response = deserialize(xhr.responseText);
|
||||||
} else {
|
} else if (
|
||||||
|
xhr.getResponseHeader("content-type").startsWith("application/json")
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
response = xhr.responseText ? JSON.parse(xhr.responseText) : null;
|
response = xhr.responseText ? JSON.parse(xhr.responseText) : null;
|
||||||
} catch (err) {
|
} 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
|
// https://mithril.js.org/request.html#error-handling
|
||||||
|
|||||||
Reference in new issue
Block a user