03e14d560d26803b9b093a0111377b4e08996ff5f55dc210262538ab3d6d29b71a74d7f3b732cb0616559030448c1b5f53789c543b9dc8124bf7a6190f9a4f 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. var utils = require('../utils');
  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. * @returns {Error} The created error.
  12. */
  13. function AxiosError(message, code, config, request, response) {
  14. Error.call(this);
  15. if (Error.captureStackTrace) {
  16. Error.captureStackTrace(this, this.constructor);
  17. } else {
  18. this.stack = (new Error()).stack;
  19. }
  20. this.message = message;
  21. this.name = 'AxiosError';
  22. code && (this.code = code);
  23. config && (this.config = config);
  24. request && (this.request = request);
  25. response && (this.response = response);
  26. }
  27. utils.inherits(AxiosError, Error, {
  28. toJSON: function toJSON() {
  29. return {
  30. // Standard
  31. message: this.message,
  32. name: this.name,
  33. // Microsoft
  34. description: this.description,
  35. number: this.number,
  36. // Mozilla
  37. fileName: this.fileName,
  38. lineNumber: this.lineNumber,
  39. columnNumber: this.columnNumber,
  40. stack: this.stack,
  41. // Axios
  42. config: this.config,
  43. code: this.code,
  44. status: this.response && this.response.status ? this.response.status : null
  45. };
  46. }
  47. });
  48. var prototype = AxiosError.prototype;
  49. var descriptors = {};
  50. [
  51. 'ERR_BAD_OPTION_VALUE',
  52. 'ERR_BAD_OPTION',
  53. 'ECONNABORTED',
  54. 'ETIMEDOUT',
  55. 'ERR_NETWORK',
  56. 'ERR_FR_TOO_MANY_REDIRECTS',
  57. 'ERR_DEPRECATED',
  58. 'ERR_BAD_RESPONSE',
  59. 'ERR_BAD_REQUEST',
  60. 'ERR_CANCELED',
  61. 'ERR_NOT_SUPPORT',
  62. 'ERR_INVALID_URL'
  63. // eslint-disable-next-line func-names
  64. ].forEach(function(code) {
  65. descriptors[code] = {value: code};
  66. });
  67. Object.defineProperties(AxiosError, descriptors);
  68. Object.defineProperty(prototype, 'isAxiosError', {value: true});
  69. // eslint-disable-next-line func-names
  70. AxiosError.from = function(error, code, config, request, response, customProps) {
  71. var axiosError = Object.create(prototype);
  72. utils.toFlatObject(error, axiosError, function filter(obj) {
  73. return obj !== Error.prototype;
  74. });
  75. AxiosError.call(axiosError, error.message, code, config, request, response);
  76. axiosError.cause = error;
  77. axiosError.name = error.name;
  78. customProps && Object.assign(axiosError, customProps);
  79. return axiosError;
  80. };
  81. module.exports = AxiosError;