8000 Fixed merge conflict · darth-coder-js/axios@340ad9e · GitHub
[go: up one dir, main page]

Skip to content

Commit 340ad9e

Browse files
committed
Fixed merge conflict
2 parents 9497729 + d8d43c1 commit 340ad9e

29 files changed

+339
-230
lines changed

index.d.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,35 @@ export interface AxiosResponse<T = any, D = any> {
136136
request?: any;
137137
}
138138

139-
export interface AxiosError<T = any, D = any> extends Error {
139+
export class AxiosError<T = unknown, D = any> extends Error {
140+
constructor(
141+
message?: string,
142+
code?: string,
143+
config?: AxiosRequestConfig<D>,
144+
request?: any,
145+
response?: AxiosResponse<T, D>
146+
);
147+
140148
config: AxiosRequestConfig<D>;
141149
code?: string;
142150
request?: any;
143151
response?: AxiosResponse<T, D>;
144152
isAxiosError: boolean;
153+
status?: string;
145154
toJSON: () => object;
155+
static readonly ERR_FR_TOO_MANY_REDIRECTS = "ERR_FR_TOO_MANY_REDIRECTS";
156+
static readonly ERR_BAD_OPTION_VALUE = "ERR_BAD_OPTION_VALUE";
157+
static readonly ERR_BAD_OPTION = "ERR_BAD_OPTION";
158+
static readonly ERR_NETWORK = "ERR_NETWORK";
159+
static readonly ERR_DEPRECATED = "ERR_DEPRECATED";
160+
static readonly ERR_BAD_RESPONSE = "ERR_BAD_RESPONSE";
161+
static readonly ERR_BAD_REQUEST = "ERR_BAD_REQUEST";
162+
static readonly ERR_CANCELED = "ERR_CANCELED";
163+
static readonly ECONNABORTED = "ECONNABORTED";
164+
static readonly ETIMEDOUT = "ETIMEDOUT";
165+
}
166+
167+
export class CanceledError<T> extends AxiosError<T> {
146168
}
147169

148170
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {

lib/adapters/http.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ var httpsFollow = require('follow-redirects').https;
1111
var url = require('url');
1212
var zlib = require('zlib');
1313
var VERSION = require('./../env/data').version;
14-
var createError = require('../core/createError');
15-
var enhanceError = require('../core/enhanceError');
16-
var transitionalDefaults = require('../defaults/transitional');
17-
var Cancel = require('../cancel/Cancel');
14+
var createError = require('../core/createError');//
15+
var enhanceError = require('../core/enhanceError');//
16+
var defaults = require('../defaults');
17+
var AxiosError = require('../core/AxiosError');
18+
var CanceledError = require('../cancel/CanceledError');
1819

1920
var isHttps = /https:?/;
2021

@@ -95,8 +96,9 @@ module.exports = function httpAdapter(config) {
9596
} else if (utils.isString(data)) {
9697
data = Buffer.from(data, 'utf-8');
9798
} else {
98-
return reject(createError(
99+
return reject(new AxiosError(
99100
'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
101+
AxiosError.ERR_BAD_REQUEST,
100102
config
101103
));
102104
}
@@ -299,8 +301,8 @@ module.exports = function httpAdapter(config) {
299301
// stream.destoy() emit aborted event before calling reject() on Node.js v16
300302
rejected = true;
301303
stream.destroy();
302-
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
303-
config, null, lastRequest));
304+
reject(new AxiosError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
305+
AxiosError.ERR_BAD_RESPONSE, config, lastRequest));
304306
}
305307
});
306308

@@ -314,7 +316,7 @@ module.exports = function httpAdapter(config) {
314316

315317
stream.on('error', function handleStreamError(err) {
316318
if (req.aborted) return;
317-
reject(enhanceError(err, config, null, lastRequest));
319+
reject(AxiosError.from(err, null, config, lastRequest));
318320
});
319321

320322
stream.on('end', function handleStreamEnd() {
@@ -337,8 +339,8 @@ module.exports = function httpAdapter(config) {
337339

338340
// Handle errors
339341
req.on('error', function handleRequestError(err) {
340-
if (req.aborted && err.code !== 'ERR_FR_TOO_MANY_REDIRECTS') reject(err);
341-
reject(enhanceError(err, config, null, req));
342+
if (req.aborted && err.code !== AxiosError.ERR_FR_TOO_MANY_REDIRECTS) return;
343+
reject(AxiosError.from(err, null, config, req));
342344
});
343345

344346
// set tcp keep alive to prevent drop connection by peer
@@ -353,10 +355,10 @@ module.exports = function httpAdapter(config) {
353355
var timeout = parseInt(config.timeout, 10);
354356

355357
if (isNaN(timeout)) {
356-
reject(createError(
358+
reject(new AxiosError(
357359
'error trying to parse `config.timeout` to int',
360+
AxiosError.ERR_BAD_OPTION_VALUE,
358361
config,
359-
'ERR_PARSE_TIMEOUT',
360362
req
361363
));
362364

@@ -370,17 +372,11 @@ module.exports = function httpAdapter(config) {
370372
// ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
371373
req.setTimeout(timeout, function handleRequestTimeout() {
372374
req.abort();
373-
var timeoutErrorMessage = '';
374-
if (config.timeoutErrorMessage) {
375-
timeoutErrorMessage = config.timeoutErrorMessage;
376-
} else {
377-
timeoutErrorMessage = 'timeout of ' + config.timeout + 'ms exceeded';
378-
}
379-
var transitional = config.transitional || transitionalDefaults;
380-
reject(createError(
381-
timeoutErrorMessage,
375+
var transitional = config.transitional || defaults.transitional;
376+
reject(new AxiosError(
377+
'timeout of ' + timeout + 'ms exceeded',
378+
transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
382379
config,
383-
transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
384380
req
385381
));
386382
});
@@ -393,7 +389,7 @@ module.exports = function httpAdapter(config) {
393389
if (req.aborted) return;
394390

395391
req.abort();
396-
reject(!cancel || (cancel && cancel.type) ? new Cancel('canceled') : cancel);
392+
reject(!cancel || (cancel && cancel.type) ? new CanceledError() : cancel);
397393
};
398394

399395
config.cancelToken && config.cancelToken.subscribe(onCanceled);
@@ -406,7 +402,7 @@ module.exports = function httpAdapter(config) {
406402
// Send the request
407403
if (utils.isStream(data)) {
408404
data.on('error', function handleStreamError(err) {
409-
reject(enhanceError(err, config, null, req));
405+
reject(AxiosError.from(err, config, null, req));
410406
}).pipe(req);
411407
} else {
412408
req.end(data);

lib/adapters/xhr.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ var buildURL = require('./../helpers/buildURL');
77
var buildFullPath = require('../core/buildFullPath');
88
var parseHeaders = require('./../helpers/parseHeaders');
99
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
10-
var createError = require('../core/createError');
10+
var createError = require('../core/createError');//
1111
var url = require('url');
1212
var transitionalDefaults = require('../defaults/transitional');
13-
var Cancel = require('../cancel/Cancel');
13+
var AxiosError = require('../core/AxiosError');
14+
var CanceledError = require('../cancel/CanceledError');
1415

1516
module.exports = function xhrAdapter(config) {
1617
return new Promise(function dispatchXhrRequest(resolve, reject) {
@@ -108,7 +109,7 @@ module.exports = function xhrAdapter(config) {
108109
return;
109110
}
110111

111-
reject(createError('Request aborted', config, 'ECONNABORTED', request));
112+
reject(new AxiosError('Request aborted', AxiosError.ECONNABORTED, config, request));
112113

113114
// Clean up request
114115
request = null;
@@ -118,7 +119,7 @@ module.exports = function xhrAdapter(config) {
118119
request.onerror = function handleError() {
119120
// Real errors are hidden from us by the browser
120121
// onerror should only fire if it's a network error
121-
reject(createError('Network Error', config, null, request));
122+
reject(new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request, request));
122123

123124
// Clean up request
124125
request = null;
@@ -131,10 +132,10 @@ module.exports = function xhrAdapter(config) {
131132
if (config.timeoutErrorMessage) {
132133
timeoutErrorMessage = config.timeoutErrorMessage;
133134
}
134-
reject(createError(
135+
reject(new AxiosError(
135136
timeoutErrorMessage,
137+
transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
136138
config,
137-
transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
138139
request));
139140

140141
// Clean up request
@@ -195,7 +196,7 @@ module.exports = function xhrAdapter(config) {
195196
if (!request) {
196197
return;
197198
}
198-
reject(!cancel || (cancel && cancel.type) ? new Cancel('canceled') : cancel);
199+
reject(!cancel || (cancel && cancel.type) ? new CanceledError() : cancel);
199200
request.abort();
200201
request = null;
201202
};

lib/axios.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ var axios = createInstance(defaults);
3737
axios.Axios = Axios;
3838

3939
// Expose Cancel & CancelToken
40-
axios.Cancel = require('./cancel/Cancel');
40+
axios.CanceledError = require('./cancel/CanceledError');
4141
axios.CancelToken = require('./cancel/CancelToken');
4242
axios.isCancel = require('./cancel/isCancel');
4343
axios.VERSION = require('./env/data').version;
4444

45+
// Expose AxiosError class
46+
axios.AxiosError = require('../lib/core/AxiosError');
47+
48+
// alias for CanceledError for backward compatibility
49+
axios.Cancel = axios.CanceledError;
50+
4551
// Expose all/spread
4652
axios.all = function all(promises) {
4753
return Promise.all(promises);

lib/cancel/Cancel.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/cancel/CancelToken.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var Cancel = require('./Cancel');
3+
var CanceledError = require('./CanceledError');
44

55
/**
66
* A `CancelToken` is an object that can be used to request cancellation of an operation.
@@ -56,13 +56,13 @@ function CancelToken(executor) {
5656
return;
5757
}
5858

59-
token.reason = new Cancel(message);
59+
token.reason = new CanceledError(message);
6060
resolvePromise(token.reason);
6161
});
6262
}
6363

6464
/**
65-
* Throws a `Cancel` if cancellation has been requested.
65+
* Throws a `CanceledError` if cancellation has been requested.
6666
*/
6767
CancelToken.prototype.throwIfRequested = function throwIfRequested() {
6868
if (this.reason) {

lib/cancel/CanceledError.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var AxiosError = require('../core/AxiosError');
4+
var utils = require('../utils');
5+
6+
/**
7+
* A `CanceledError` is an object that is thrown when an operation is canceled.
8+
*
9+
* @class
10+
* @param {string=} message The message.
11+
*/
12+
function CanceledError(message) {
13+
// eslint-disable-next-line no-eq-null,eqeqeq
14+
AxiosError.call(this, message == null ? 'canceled' : message, AxiosError.ERR_CANCELED);
15+
this.name = 'CanceledError';
16+
}
17+
18+
utils.inherits(CanceledError, AxiosError, {
19+
__CANCEL__: true
20+
});
21+
22+
module.exports = CanceledError;

lib/core/AxiosError.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
var utils = require('../utils');
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+
* @returns {Error} The created error.
14+
*/
15+
function AxiosError(message, code, config, request, response) {
16+
Error.call(this);
17+
this.message = message;
18+
this.name = 'AxiosError';
19+
code && (this.code = code);
20+
config && (this.config = config);
21+
request && (this.request = request);
22+
response && (this.response = response);
23+
}
24+
25+
utils.inherits(AxiosError, Error, {
26+
toJSON: function toJSON() {
27+
return {
28+
// Standard
29+
message: this.message,
30+
name: this.name,
31+
// Microsoft
32+
description: this.description,
33+
number: this.number,
34+
// Mozilla
35+
fileName: this.fileName,
36+
lineNumber: this.lineNumber,
37+
columnNumber: this.columnNumber,
38+
stack: this.stack,
39+
// Axios
40+
config: this.config,
41+
code: this.code,
42+
status: this.response && this.response.status ? this.response.status : null
43+
};
44+
}
45+
});
46+
47+
var prototype = AxiosError.prototype;
48+
var descriptors = {};
49+
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+
// eslint-disable-next-line func-names
62+
].forEach(function(code) {
63+
descriptors[code] = {value: code};
64+
});
65+
66+
Object.defineProperties(AxiosError, descriptors);
67+
Object.defineProperty(prototype, 'isAxiosError', {value: true});
68+
69+
// eslint-disable-next-line func-names
70+
AxiosError.from = function(error, code, config, request, response, customProps) {
71+
var axiosError = Object.create(prototype);
72+
73+
utils.toFlatObject(error, axiosError, function filter(obj) {
74+
return obj !== Error.prototype;
75+
});
76+
77+
AxiosError.call(axiosError, error.message, code, config, request, response);
78+
79+
axiosError.name = error.name;
80+
81+
customProps && Object.assign(axiosError, customProps);
82+
83+
return axiosError;
84+
};
85+
86+
module.exports = AxiosError;

lib/core/createError.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0