This how I call fetch
fetchOpts = {...}
return fetch(url, fetchOpts)
.then(checkStatus)
The url I call will always return a json data, even in the error condition:
{error_message:"Encountered issue with update"
status:"error",
status_code:400}
I would like to get hold of the error_message
field value and pass to the Error
constructor.
Inside checkStatus, it handles exception this way:
function checkStatus(response) {
let error = null;
if (!response.headers.get('Content-Type').includes('application/json')) {
error = new Error(response.statusText);
error.response = response;
throw error;
}
if (response.status >= 200 && response.status < 300) {
return response;
}
if (response.ok) {
return response;
}
const jsonData = response.json() ; // <- `jsonData` is a promise
error = new Error(/* How to extract error_message from json data? */);
error.response = response;
throw error;
}
I have checked this question already but it does not really address my need fetch: Reject promise with JSON error object. Also the accepted answer seems, based on the comments, not to resolve OP's question entirely.