I am trying to intelligently handle the success/error responses from our API using fetch & ES6 promises.
Here is how I need to handle response statuses:
I think you can write it out pretty easily:
fetch(…).then(response => {
if (response.ok)
return response[response.status == 204 ? "text" : "json"]();
if (response.status == 422)
return response.json().then(err => { throw err; });
if (response.status == 406)
var error = new AuthentificationError(response.statusText); // or whatever
else
var error = new Error(response.statusText)
error.response = response
throw error;
})