I'm trying to figure out how to process failed http responses in the fetch example in reason-react-example repo.
The following was my first idea (tinkering with the url):
Js.Promise.(
Fetch.fetch("https://dog.ceo/api/breeds/list")
|> then_(res => {
let ok = Fetch.Response.ok(res);
ok ?
Fetch.Response.json(res) :
Js.Exn.raiseError(Fetch.Response.statusText(res));
})
|> then_(json =>
json
|> Decode.dogs
|> (dogs => self.send(DogsFetched(dogs)))
|> resolve
)
|> catch(err => {
Js.log(err);
[%bs.raw {| console.log(err.response) |}]
Js.Promise.resolve(self.send(DogsFailedToFetch));
})
|> ignore
);
It's not working as I hoped. It turns out Fetch rejects right away when the HTTP request fails with 404 for instance, which I didn't expect since it's not how the browser fetch API works. Moreover, when err is logged it's TypeError: Failed to fetch and the err.response property is undefined.
My question is: how to process the error to get the status code and status text for example?