source: node_modules/axios/lib/core/AxiosError.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1'use strict';
2
3import utils from '../utils.js';
4
5/**
6 * Create an Error with the specified message, config, error code, request and response.
7 *
8 * @param {string} message The error message.
9 * @param {string} [code] The error code (for example, 'ECONNABORTED').
10 * @param {Object} [config] The config.
11 * @param {Object} [request] The request.
12 * @param {Object} [response] The response.
13 *
14 * @returns {Error} The created error.
15 */
16function AxiosError(message, code, config, request, response) {
17 Error.call(this);
18
19 if (Error.captureStackTrace) {
20 Error.captureStackTrace(this, this.constructor);
21 } else {
22 this.stack = (new Error()).stack;
23 }
24
25 this.message = message;
26 this.name = 'AxiosError';
27 code && (this.code = code);
28 config && (this.config = config);
29 request && (this.request = request);
30 response && (this.response = response);
31}
32
33utils.inherits(AxiosError, Error, {
34 toJSON: function toJSON() {
35 return {
36 // Standard
37 message: this.message,
38 name: this.name,
39 // Microsoft
40 description: this.description,
41 number: this.number,
42 // Mozilla
43 fileName: this.fileName,
44 lineNumber: this.lineNumber,
45 columnNumber: this.columnNumber,
46 stack: this.stack,
47 // Axios
48 config: utils.toJSONObject(this.config),
49 code: this.code,
50 status: this.response && this.response.status ? this.response.status : null
51 };
52 }
53});
54
55const prototype = AxiosError.prototype;
56const descriptors = {};
57
58[
59 'ERR_BAD_OPTION_VALUE',
60 'ERR_BAD_OPTION',
61 'ECONNABORTED',
62 'ETIMEDOUT',
63 'ERR_NETWORK',
64 'ERR_FR_TOO_MANY_REDIRECTS',
65 'ERR_DEPRECATED',
66 'ERR_BAD_RESPONSE',
67 'ERR_BAD_REQUEST',
68 'ERR_CANCELED',
69 'ERR_NOT_SUPPORT',
70 'ERR_INVALID_URL'
71// eslint-disable-next-line func-names
72].forEach(code => {
73 descriptors[code] = {value: code};
74});
75
76Object.defineProperties(AxiosError, descriptors);
77Object.defineProperty(prototype, 'isAxiosError', {value: true});
78
79// eslint-disable-next-line func-names
80AxiosError.from = (error, code, config, request, response, customProps) => {
81 const axiosError = Object.create(prototype);
82
83 utils.toFlatObject(error, axiosError, function filter(obj) {
84 return obj !== Error.prototype;
85 }, prop => {
86 return prop !== 'isAxiosError';
87 });
88
89 AxiosError.call(axiosError, error.message, code, config, request, response);
90
91 axiosError.cause = error;
92
93 axiosError.name = error.name;
94
95 customProps && Object.assign(axiosError, customProps);
96
97 return axiosError;
98};
99
100export default AxiosError;
Note: See TracBrowser for help on using the repository browser.