function handleErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
// then use as below
fetch("http://httpstat.us/500")
.then(handleErrors)
.then(response => console.log("ok") )
.catch(error => console.log(error) );
function CheckError(response) {
if (response.ok) {
return response.json();
} else {
throw Error(response.statusText);
}
}
// Now call the function inside fetch promise resolver
fetch(url)
.then(CheckError)
.then((jsonResponse) => {
}).catch((error) => {
});
json api fetch not working