sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.27k stars 935 forks source link

`beforeError` hook does not return mutated error object after got 11.6.0 #1528

Closed piotrjaromin closed 3 years ago

piotrjaromin commented 3 years ago

Describe the bug

Untill version 11.5.0 hooks.beforeError was able to return modified error instance, but after bump to 11.6.0 this is functionality is lost, error returned is always RequestError from got library.

If I am correct the overridden error comes from this line: https://github.com/sindresorhus/got/blob/49c16ee54fb19ea7aa77e24ac8c2b602f0aad265/source/core/index.ts#L2708

Actual behavior

errors returned by before hook are NOT propagated to caller

Expected behavior

errors returned by before hook propagated to caller

Code to reproduce

const got = require('got');

class CustomError extends Error {}

got('https://api.github.com/some-endpoint', {
    hooks: {
        beforeError: [err => {
            console.log('Mapping to custom one:', err.message);
            return new CustomError();
        }],
    },
})
    .then(() => console.log('Error was not thrown')) // <- will not be logged
    // Below line for got:
    // 11.5.0: Got error: CustomError
    // 11.6.0: Got error: RequestError
    .catch(err => console.log('Got error:', err.constructor.name));
...

Checklist

Batuzz commented 3 years ago

I have exactly the same actual/expected behavior on:

szmarczak commented 3 years ago

I agree that it would be great to see this feature. There is already an issue about this.

Duplicate of #1353