AxiosError.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. import utils from '../utils.js';
  3. /**
  4. * Create an Error with the specified message, config, error code, request and response.
  5. *
  6. * @param {string} message The error message.
  7. * @param {string} [code] The error code (for example, 'ECONNABORTED').
  8. * @param {Object} [config] The config.
  9. * @param {Object} [request] The request.
  10. * @param {Object} [response] The response.
  11. *
  12. * @returns {Error} The created error.
  13. */
  14. function AxiosError(message, code, config, request, response) {
  15. Error.call(this);
  16. if (Error.captureStackTrace) {
  17. Error.captureStackTrace(this, this.constructor);
  18. } else {
  19. this.stack = (new Error()).stack;
  20. }
  21. this.message = message;
  22. this.name = 'AxiosError';
  23. code && (this.code = code);
  24. config && (this.config = config);
  25. request && (this.request = request);
  26. if (response) {
  27. this.response = response;
  28. this.status = response.status ? response.status : null;
  29. }
  30. }
  31. utils.inherits(AxiosError, Error, {
  32. toJSON: function toJSON() {
  33. return {
  34. // Standard
  35. message: this.message,
  36. name: this.name,
  37. // Microsoft
  38. description: this.description,
  39. number: this.number,
  40. // Mozilla
  41. fileName: this.fileName,
  42. lineNumber: this.lineNumber,
  43. columnNumber: this.columnNumber,
  44. stack: this.stack,
  45. // Axios
  46. config: utils.toJSONObject(this.config),
  47. code: this.code,
  48. status: this.status
  49. };
  50. }
  51. });
  52. const prototype = AxiosError.prototype;
  53. const descriptors = {};
  54. [
  55. 'ERR_BAD_OPTION_VALUE',
  56. 'ERR_BAD_OPTION',
  57. 'ECONNABORTED',
  58. 'ETIMEDOUT',
  59. 'ERR_NETWORK',
  60. 'ERR_FR_TOO_MANY_REDIRECTS',
  61. 'ERR_DEPRECATED',
  62. 'ERR_BAD_RESPONSE',
  63. 'ERR_BAD_REQUEST',
  64. 'ERR_CANCELED',
  65. 'ERR_NOT_SUPPORT',
  66. 'ERR_INVALID_URL'
  67. // eslint-disable-next-line func-names
  68. ].forEach(code => {
  69. descriptors[code] = {value: code};
  70. });
  71. Object.defineProperties(AxiosError, descriptors);
  72. Object.defineProperty(prototype, 'isAxiosError', {value: true});
  73. // eslint-disable-next-line func-names
  74. AxiosError.from = (error, code, config, request, response, customProps) => {
  75. const axiosError = Object.create(prototype);
  76. utils.toFlatObject(error, axiosError, function filter(obj) {
  77. return obj !== Error.prototype;
  78. }, prop => {
  79. return prop !== 'isAxiosError';
  80. });
  81. AxiosError.call(axiosError, error.message, code, config, request, response);
  82. axiosError.cause = error;
  83. axiosError.name = error.name;
  84. customProps && Object.assign(axiosError, customProps);
  85. return axiosError;
  86. };
  87. export default AxiosError;