summaryrefslogtreecommitdiff
path: root/lib/core/enhanceError.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/core/enhanceError.js')
-rw-r--r--lib/core/enhanceError.js42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/core/enhanceError.js b/lib/core/enhanceError.js
new file mode 100644
index 0000000..b6bc444
--- /dev/null
+++ b/lib/core/enhanceError.js
@@ -0,0 +1,42 @@
+'use strict';
+
+/**
+ * Update an Error with the specified config, error code, and response.
+ *
+ * @param {Error} error The error to update.
+ * @param {Object} config The config.
+ * @param {string} [code] The error code (for example, 'ECONNABORTED').
+ * @param {Object} [request] The request.
+ * @param {Object} [response] The response.
+ * @returns {Error} The error.
+ */
+module.exports = function enhanceError(error, config, code, request, response) {
+ error.config = config;
+ if (code) {
+ error.code = code;
+ }
+
+ error.request = request;
+ error.response = response;
+ error.isAxiosError = true;
+
+ error.toJSON = function toJSON() {
+ return {
+ // Standard
+ message: this.message,
+ name: this.name,
+ // Microsoft
+ description: this.description,
+ number: this.number,
+ // Mozilla
+ fileName: this.fileName,
+ lineNumber: this.lineNumber,
+ columnNumber: this.columnNumber,
+ stack: this.stack,
+ // Axios
+ config: this.config,
+ code: this.code
+ };
+ };
+ return error;
+};