tapable
tapable copied to clipboard
[Bug] Async hook doesn't catch error when reject a falsy value
Hi, I seem to have found a bug:
Environment
- Tapable: v2.2.1
- Node: v16.16.0
- OS: MacOS 12.5
Case
const { AsyncSeriesHook } = require('tapable');
const hook = new AsyncSeriesHook();
hook.tapPromise('A', () => {
return Promise.reject(0); // reject a falsy value
});
hook.tapPromise('B', () => {
console.log('some thing');
});
hook.callAsync((err) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Success');
}
});
Expected output: Error: 0
But the actual output: Success
Hm, why you pass 0 in reject?
This is just an example, my real case is: Project.reject() , reject a undefined
Can you try to use Promise.reject(new Error("test"))?
Of course, I can somehow ignore this case, but I think this can be made more robust. I don't use hooks directly, and I expose them to others, so I have to fix. Below shows how I fixed it. and I hope the official can fix it
function _fixTapablePromiseHook(hook: Hook<any, any>) {
hook.intercept({
register: (tapInfo) => {
const originalFn = tapInfo.fn;
if (tapInfo.type === 'promise') {
tapInfo.fn = async function (...args: any[]) {
try {
return await originalFn.apply(tapInfo, args);
} catch (err) {
if (!err) err = new Error();
throw err;
}
};
}
return tapInfo;
},
});
}
hm, looks good, feel free to send a PR
Hi, I would like to take this issue !!