Skip to content

Commit

Permalink
fix(http): fixed regression bug when handling synchronous errors insi…
Browse files Browse the repository at this point in the history
…de the adapter; (#5564)
  • Loading branch information
DigitalBrainJS committed Feb 22, 2023
1 parent d9ebf8f commit a3b246c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
58 changes: 33 additions & 25 deletions lib/adapters/http.js
Expand Up @@ -116,26 +116,47 @@ function setProxy(options, configProxy, location) {

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*/
export default isHttpAdapterSupported && function httpAdapter(config) {
/*eslint no-async-promise-executor:0*/
return new Promise(async function dispatchHttpRequest(resolvePromise, rejectPromise) {
let data = config.data;
const responseType = config.responseType;
const responseEncoding = config.responseEncoding;
return wrapAsync(async function dispatchHttpRequest(resolve, reject, onDone) {
let {data} = config;
const {responseType, responseEncoding} = config;
const method = config.method.toUpperCase();
let isFinished;
let isDone;
let rejected = false;
let req;

// temporary internal emitter until the AxiosRequest class will be implemented
const emitter = new EventEmitter();

function onFinished() {
if (isFinished) return;
isFinished = true;

const onFinished = () => {
if (config.cancelToken) {
config.cancelToken.unsubscribe(abort);
}
Expand All @@ -147,26 +168,13 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
emitter.removeAllListeners();
}

function done(value, isRejected) {
if (isDone) return;

onDone((value, isRejected) => {
isDone = true;

if (isRejected) {
rejected = true;
onFinished();
}

isRejected ? rejectPromise(value) : resolvePromise(value);
}

const resolve = function resolve(value) {
done(value);
};

const reject = function reject(value) {
done(value, true);
};
});

function abort(reason) {
emitter.emit('abort', !reason || reason.type ? new CanceledError(null, config, req) : reason);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -203,4 +203,4 @@
"@commitlint/config-conventional"
]
}
}
}
4 changes: 4 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -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/);
});
});

0 comments on commit a3b246c

Please sign in to comment.