3

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.

inferno
  • 31
  • 1
  • Maybe also see [this](http://stackoverflow.com/q/37555031/1048572) or [that](http://stackoverflow.com/q/33907465/1048572) – Bergi Oct 12 '16 at 11:18

0 Answers0