2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

fix(http): fixed regression bug when handling synchronous errors inside the adapter; (#5564)

This commit is contained in:
Dmitriy Mozgovoy
2023-02-22 21:50:31 +02:00
committed by GitHub
parent d9ebf8fb3a
commit a3b246c9de
3 changed files with 38 additions and 26 deletions
+33 -25
View File
@@ -116,15 +116,39 @@ function setProxy(options, configProxy, location) {
const isHttpAdapterSupported = typeof process !== 'undefined' && utils.kindOf(process) === 'process'; const isHttpAdapterSupported = typeof process !== 'undefined' && utils.kindOf(process) === 'process';
// temporary hotfix
const wrapAsync = (asyncExecutor) => {
return new Promise((resolve, reject) => {
let onDone;
let isDone;
const done = (value, isRejected) => {
if (isDone) return;
isDone = true;
onDone && onDone(value, isRejected);
}
const _resolve = (value) => {
done(value);
resolve(value);
};
const _reject = (reason) => {
done(reason, true);
reject(reason);
}
asyncExecutor(_resolve, _reject, (onDoneHandler) => (onDone = onDoneHandler)).catch(_reject);
})
};
/*eslint consistent-return:0*/ /*eslint consistent-return:0*/
export default isHttpAdapterSupported && function httpAdapter(config) { export default isHttpAdapterSupported && function httpAdapter(config) {
/*eslint no-async-promise-executor:0*/ return wrapAsync(async function dispatchHttpRequest(resolve, reject, onDone) {
return new Promise(async function dispatchHttpRequest(resolvePromise, rejectPromise) { let {data} = config;
let data = config.data; const {responseType, responseEncoding} = config;
const responseType = config.responseType;
const responseEncoding = config.responseEncoding;
const method = config.method.toUpperCase(); const method = config.method.toUpperCase();
let isFinished;
let isDone; let isDone;
let rejected = false; let rejected = false;
let req; let req;
@@ -132,10 +156,7 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
// temporary internal emitter until the AxiosRequest class will be implemented // temporary internal emitter until the AxiosRequest class will be implemented
const emitter = new EventEmitter(); const emitter = new EventEmitter();
function onFinished() { const onFinished = () => {
if (isFinished) return;
isFinished = true;
if (config.cancelToken) { if (config.cancelToken) {
config.cancelToken.unsubscribe(abort); config.cancelToken.unsubscribe(abort);
} }
@@ -147,26 +168,13 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
emitter.removeAllListeners(); emitter.removeAllListeners();
} }
function done(value, isRejected) { onDone((value, isRejected) => {
if (isDone) return;
isDone = true; isDone = true;
if (isRejected) { if (isRejected) {
rejected = true; rejected = true;
onFinished(); onFinished();
} }
});
isRejected ? rejectPromise(value) : resolvePromise(value);
}
const resolve = function resolve(value) {
done(value);
};
const reject = function reject(value) {
done(value, true);
};
function abort(reason) { function abort(reason) {
emitter.emit('abort', !reason || reason.type ? new CanceledError(null, config, req) : reason); emitter.emit('abort', !reason || reason.type ? new CanceledError(null, config, req) : reason);
+1 -1
View File
@@ -203,4 +203,4 @@
"@commitlint/config-conventional" "@commitlint/config-conventional"
] ]
} }
} }
+4
View File
@@ -2099,4 +2099,8 @@ describe('supports http with nodejs', function () {
} }
}); });
}) })
it('should properly handle synchronous errors inside the adapter', function () {
return assert.rejects(() => axios.get('http://192.168.0.285'), /Invalid URL/);
});
}); });