Axios: Catch does not return Error

Created on 2 Mar 2016  ·  3Comments  ·  Source: axios/axios

Why does catch return an object and not an error object?

Most helpful comment

This is how it is done with Angular's $http, which this library is based on. More or less it's an artifact and not necessarily the right way to do things. I would like to move to a more consistent error path where an Error is used that has response details attached if relevant.

function createError(message, details) {
  var err = new Error(message);

  Object.keys(details, function (key) {
    err[key] = details[key];
  });

  return err;
}

createError('timeout of ' + config.timeout + 'ms exceeded', {
  code: 'ECONNABORTED',
  config: config
});

I would also like to change the lib to only reject the Promise when an actual error occurs, regardless of HTTP status code. Whereas now anything outside the 200-300 range is rejected.

All 3 comments

This is how it is done with Angular's $http, which this library is based on. More or less it's an artifact and not necessarily the right way to do things. I would like to move to a more consistent error path where an Error is used that has response details attached if relevant.

function createError(message, details) {
  var err = new Error(message);

  Object.keys(details, function (key) {
    err[key] = details[key];
  });

  return err;
}

createError('timeout of ' + config.timeout + 'ms exceeded', {
  code: 'ECONNABORTED',
  config: config
});

I would also like to change the lib to only reject the Promise when an actual error occurs, regardless of HTTP status code. Whereas now anything outside the 200-300 range is rejected.

@mzabriskie
You could also consider the use of a config flag to determine what action to take when a non 2xx/3xx response is returned.

The request-promise lib uses the simple flag/option for this. When simple is true, request-promise rejects on non-network errors (2xx, 3xx). When the flag is false, it rejects nothing and becomes the user's responsibility. I've found this to be very useful and have applications that use both settings. I think I commonly have simple set to true as it is generally a better default IMO.

https://github.com/request/request-promise#get-a-rejection-only-if-the-request-failed-for-technical-reasons

Closing as duplicate of #24

Was this page helpful?
0 / 5 - 0 ratings

Related issues

9tor picture 9tor  ·  3Comments

ghost picture ghost  ·  3Comments

samayo picture samayo  ·  3Comments

helmus picture helmus  ·  3Comments

altruisticsoftware picture altruisticsoftware  ·  3Comments