I’m using JSON.parse
on a response that sometimes contains a 404 response. In the cases where it returns 404, is there a way to catch an exception and then exec
We can check error & 404 statusCode, and use try {} catch (err) {}
.
You can try this :
const req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.status == 404) {
console.log("404");
return false;
}
if (!(req.readyState == 4 && req.status == 200))
return false;
const json = (function(raw) {
try {
return JSON.parse(raw);
} catch (err) {
return false;
}
})(req.responseText);
if (!json)
return false;
document.body.innerHTML = "Your city : " + json.city + "
Your isp : " + json.org;
};
req.open("GET", "https://ipapi.co/json/", true);
req.send();
Read more :