1 | 'use strict';
|
---|
2 |
|
---|
3 | import 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 | */
|
---|
16 | function 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 | if (response) {
|
---|
31 | this.response = response;
|
---|
32 | this.status = response.status ? response.status : null;
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | utils.inherits(AxiosError, Error, {
|
---|
37 | toJSON: function toJSON() {
|
---|
38 | return {
|
---|
39 | // Standard
|
---|
40 | message: this.message,
|
---|
41 | name: this.name,
|
---|
42 | // Microsoft
|
---|
43 | description: this.description,
|
---|
44 | number: this.number,
|
---|
45 | // Mozilla
|
---|
46 | fileName: this.fileName,
|
---|
47 | lineNumber: this.lineNumber,
|
---|
48 | columnNumber: this.columnNumber,
|
---|
49 | stack: this.stack,
|
---|
50 | // Axios
|
---|
51 | config: utils.toJSONObject(this.config),
|
---|
52 | code: this.code,
|
---|
53 | status: this.status
|
---|
54 | };
|
---|
55 | }
|
---|
56 | });
|
---|
57 |
|
---|
58 | const prototype = AxiosError.prototype;
|
---|
59 | const descriptors = {};
|
---|
60 |
|
---|
61 | [
|
---|
62 | 'ERR_BAD_OPTION_VALUE',
|
---|
63 | 'ERR_BAD_OPTION',
|
---|
64 | 'ECONNABORTED',
|
---|
65 | 'ETIMEDOUT',
|
---|
66 | 'ERR_NETWORK',
|
---|
67 | 'ERR_FR_TOO_MANY_REDIRECTS',
|
---|
68 | 'ERR_DEPRECATED',
|
---|
69 | 'ERR_BAD_RESPONSE',
|
---|
70 | 'ERR_BAD_REQUEST',
|
---|
71 | 'ERR_CANCELED',
|
---|
72 | 'ERR_NOT_SUPPORT',
|
---|
73 | 'ERR_INVALID_URL'
|
---|
74 | // eslint-disable-next-line func-names
|
---|
75 | ].forEach(code => {
|
---|
76 | descriptors[code] = {value: code};
|
---|
77 | });
|
---|
78 |
|
---|
79 | Object.defineProperties(AxiosError, descriptors);
|
---|
80 | Object.defineProperty(prototype, 'isAxiosError', {value: true});
|
---|
81 |
|
---|
82 | // eslint-disable-next-line func-names
|
---|
83 | AxiosError.from = (error, code, config, request, response, customProps) => {
|
---|
84 | const axiosError = Object.create(prototype);
|
---|
85 |
|
---|
86 | utils.toFlatObject(error, axiosError, function filter(obj) {
|
---|
87 | return obj !== Error.prototype;
|
---|
88 | }, prop => {
|
---|
89 | return prop !== 'isAxiosError';
|
---|
90 | });
|
---|
91 |
|
---|
92 | AxiosError.call(axiosError, error.message, code, config, request, response);
|
---|
93 |
|
---|
94 | axiosError.cause = error;
|
---|
95 |
|
---|
96 | axiosError.name = error.name;
|
---|
97 |
|
---|
98 | customProps && Object.assign(axiosError, customProps);
|
---|
99 |
|
---|
100 | return axiosError;
|
---|
101 | };
|
---|
102 |
|
---|
103 | export default AxiosError;
|
---|