AxiosError.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. response && (this.response = response);
  27. }
  28. utils.inherits(AxiosError, Error, {
  29. toJSON: function toJSON() {
  30. return {
  31. // Standard
  32. message: this.message,
  33. name: this.name,
  34. // Microsoft
  35. description: this.description,
  36. number: this.number,
  37. // Mozilla
  38. fileName: this.fileName,
  39. lineNumber: this.lineNumber,
  40. columnNumber: this.columnNumber,
  41. stack: this.stack,
  42. // Axios
  43. config: utils.toJSONObject(this.config),
  44. code: this.code,
  45. status: this.response && this.response.status ? this.response.status : null
  46. };
  47. }
  48. });
  49. const prototype = AxiosError.prototype;
  50. const descriptors = {};
  51. [
  52. 'ERR_BAD_OPTION_VALUE',
  53. 'ERR_BAD_OPTION',
  54. 'ECONNABORTED',
  55. 'ETIMEDOUT',
  56. 'ERR_NETWORK',
  57. 'ERR_FR_TOO_MANY_REDIRECTS',
  58. 'ERR_DEPRECATED',
  59. 'ERR_BAD_RESPONSE',
  60. 'ERR_BAD_REQUEST',
  61. 'ERR_CANCELED',
  62. 'ERR_NOT_SUPPORT',
  63. 'ERR_INVALID_URL'
  64. // eslint-disable-next-line func-names
  65. ].forEach(code => {
  66. descriptors[code] = {value: code};
  67. });
  68. Object.defineProperties(AxiosError, descriptors);
  69. Object.defineProperty(prototype, 'isAxiosError', {value: true});
  70. // eslint-disable-next-line func-names
  71. AxiosError.from = (error, code, config, request, response, customProps) => {
  72. const axiosError = Object.create(prototype);
  73. utils.toFlatObject(error, axiosError, function filter(obj) {
  74. return obj !== Error.prototype;
  75. }, prop => {
  76. return prop !== 'isAxiosError';
  77. });
  78. AxiosError.call(axiosError, error.message, code, config, request, response);
  79. axiosError.cause = error;
  80. axiosError.name = error.name;
  81. customProps && Object.assign(axiosError, customProps);
  82. return axiosError;
  83. };
  84. export default AxiosError;