developit / redaxios

The Axios API, as an 800 byte Fetch wrapper.
https://npm.im/redaxios
Apache License 2.0
4.72k stars 100 forks source link

Wrap rejected response, to stay in line with Axios API #90

Open notpushkin opened 2 years ago

notpushkin commented 2 years ago

Axios returns a custom object when request status is not OK (https://github.com/axios/axios/#handling-errors):

axios.get('/user/12345')
  .catch(function (error) {
    if (error.response) {
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    }
    // ...
  })

Redaxios on the other hand throws the response directly, as pointed out in comment https://github.com/developit/redaxios/issues/48#issuecomment-958745702.

The easiest fix would be just to wrap the response in an object, but for better compatibility we could add some other fields.

developit commented 1 year ago

LGTM - I might try to refactor this to reject with an Error that has the .response property attached (I assume that's what Axios is doing). The current .catch(Object) trick likely has to go though.

notpushkin commented 1 year ago

@developit Sorry for bothering – any updates on this? :-)