enhanceError.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. /**
  3. * Update an Error with the specified config, error code, and response.
  4. *
  5. * @param {Error} error The error to update.
  6. * @param {Object} config The config.
  7. * @param {string} [code] The error code (for example, 'ECONNABORTED').
  8. * @param {Object} [request] The request.
  9. * @param {Object} [response] The response.
  10. * @returns {Error} The error.
  11. */
  12. module.exports = function enhanceError(error, config, code, request, response) {
  13. error.config = config;
  14. if (code) {
  15. error.code = code;
  16. }
  17. error.request = request;
  18. error.response = response;
  19. error.isAxiosError = true;
  20. error.toJSON = function toJSON() {
  21. return {
  22. // Standard
  23. message: this.message,
  24. name: this.name,
  25. // Microsoft
  26. description: this.description,
  27. number: this.number,
  28. // Mozilla
  29. fileName: this.fileName,
  30. lineNumber: this.lineNumber,
  31. columnNumber: this.columnNumber,
  32. stack: this.stack,
  33. // Axios
  34. config: this.config,
  35. code: this.code,
  36. status: this.response && this.response.status ? this.response.status : null
  37. };
  38. };
  39. return error;
  40. };