I tried searching for an answer to this problem but I haven't found it, sorry if it's duplicated
I have a simple app with a website in REACT and java backend, where the site gets a json from the backend and shows something with it. The problem is when the backend returns an error message, for example a 503 http response with the following body:
{"status":503,"name":"InternalServerErrorException","message":"3rd party service is not available"}
The thing is, fetch() only gets information about the http status code when response.ok is false. I can't get a statusMessage or the json returned from the backend (I tried with response.body but is a locked stream, it seems I'm not able to read it)
The fetch code is:
handleErrors = (response) => {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
};
submit = (form) => {
this.setState({ response: null, loading: true, error: false });
fetch(this.getUrl(form))
.then(this.handleErrors)
.then((clusterList) => {
return clusterList.json();
})
.then(clusterJson => this.setState({response: clusterJson, loading: false}))
.catch((e) => {
console.log(e);
this.setState({ error: true, loading: false, errorM: e.message});
});
};
I tried several things I found in other posts and tutorials but nothing seems to work. And I can't make every error message into a different error code, i.e. there are many errors for "bad input" that differs on the message.
Is there any way to get the extra information of the error?