AxiosError.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict';
  2. import utils from '../utils.js';
  3. class AxiosError extends Error {
  4. static from(error, code, config, request, response, customProps) {
  5. const axiosError = new AxiosError(error.message, code || error.code, config, request, response);
  6. axiosError.cause = error;
  7. axiosError.name = error.name;
  8. // Preserve status from the original error if not already set from response
  9. if (error.status != null && axiosError.status == null) {
  10. axiosError.status = error.status;
  11. }
  12. customProps && Object.assign(axiosError, customProps);
  13. return axiosError;
  14. }
  15. /**
  16. * Create an Error with the specified message, config, error code, request and response.
  17. *
  18. * @param {string} message The error message.
  19. * @param {string} [code] The error code (for example, 'ECONNABORTED').
  20. * @param {Object} [config] The config.
  21. * @param {Object} [request] The request.
  22. * @param {Object} [response] The response.
  23. *
  24. * @returns {Error} The created error.
  25. */
  26. constructor(message, code, config, request, response) {
  27. super(message);
  28. // Make message enumerable to maintain backward compatibility
  29. // The native Error constructor sets message as non-enumerable,
  30. // but axios < v1.13.3 had it as enumerable
  31. Object.defineProperty(this, 'message', {
  32. value: message,
  33. enumerable: true,
  34. writable: true,
  35. configurable: true
  36. });
  37. this.name = 'AxiosError';
  38. this.isAxiosError = true;
  39. code && (this.code = code);
  40. config && (this.config = config);
  41. request && (this.request = request);
  42. if (response) {
  43. this.response = response;
  44. this.status = response.status;
  45. }
  46. }
  47. toJSON() {
  48. return {
  49. // Standard
  50. message: this.message,
  51. name: this.name,
  52. // Microsoft
  53. description: this.description,
  54. number: this.number,
  55. // Mozilla
  56. fileName: this.fileName,
  57. lineNumber: this.lineNumber,
  58. columnNumber: this.columnNumber,
  59. stack: this.stack,
  60. // Axios
  61. config: utils.toJSONObject(this.config),
  62. code: this.code,
  63. status: this.status,
  64. };
  65. }
  66. }
  67. // This can be changed to static properties as soon as the parser options in .eslint.cjs are updated.
  68. AxiosError.ERR_BAD_OPTION_VALUE = 'ERR_BAD_OPTION_VALUE';
  69. AxiosError.ERR_BAD_OPTION = 'ERR_BAD_OPTION';
  70. AxiosError.ECONNABORTED = 'ECONNABORTED';
  71. AxiosError.ETIMEDOUT = 'ETIMEDOUT';
  72. AxiosError.ERR_NETWORK = 'ERR_NETWORK';
  73. AxiosError.ERR_FR_TOO_MANY_REDIRECTS = 'ERR_FR_TOO_MANY_REDIRECTS';
  74. AxiosError.ERR_DEPRECATED = 'ERR_DEPRECATED';
  75. AxiosError.ERR_BAD_RESPONSE = 'ERR_BAD_RESPONSE';
  76. AxiosError.ERR_BAD_REQUEST = 'ERR_BAD_REQUEST';
  77. AxiosError.ERR_CANCELED = 'ERR_CANCELED';
  78. AxiosError.ERR_NOT_SUPPORT = 'ERR_NOT_SUPPORT';
  79. AxiosError.ERR_INVALID_URL = 'ERR_INVALID_URL';
  80. export default AxiosError;