I am trying to fetch
a URL that returns a 400+ HTTP error code, along with some additional error reporting information in the body of the response.
return fetch(resourcePath, config)
.then(response => {
if (response.ok) return response.json();
const nonHTTPError = new Error(response.statusText);
nonHTTPError.response = response;
throw nonHTTPError;
})
.catch(err => {
const { status, statusText, urlErr } = err.response;
const errorText = `Unexpected error ${status} ${statusText}`;
return dispatch(errAction(actionType, errorText));
});
If the response is an error, calling response.json()
will return a Promise that contains the body of the response, but I'm not sure how to access the actual JSON object since it's encapsulated in a promise.
I'd like to be able to use the response body to display better error feedback text.