-
-
Notifications
You must be signed in to change notification settings - Fork 916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_URL]: Invalid URL #604
Comments
Sadly I hit my time limit for this issue. I have a fix, but sadly no test. So got tries to helpfully redirect but in constructing the redirect URL blows up. diff --git a/source/request-as-event-emitter.js b/source/request-as-event-emitter.js
index 3187657..6bb9454 100644
--- a/source/request-as-event-emitter.js
+++ b/source/request-as-event-emitter.js
@@ -120,7 +120,12 @@ module.exports = options => {
}
const bufferString = Buffer.from(response.headers.location, 'binary').toString();
- redirectUrl = (new URL(bufferString, urlLib.format(options))).toString();
+ try {
+ redirectUrl = (new URL(bufferString, urlLib.format(options))).toString();
+ } catch (error) {
+ emitter.emit('error', new RequestError(error, options));
+ return;
+ }
try {
decodeURI(redirectUrl); Sadly, in trying to write the test for it I've gotten stuck on figuring out why the test gets stuck. The following hangs: test.only('bad URL should not result in unhandled exception', async t => {
await t.throwsAsync(() => got('//'));
}) Do the same in Node: For the one adding in a proper test here's what I have so far, it includes a quick curl exec to verify the new Add this to the s.on('/invalid-redirect', (_, response) => {
response.writeHead(302, {
Location: '//'
});
response.end();
}); Test const {exec} = require('child_process');
test('bad redirect should not result in unhandled exception', async t => {
console.log(s.url)
exec(`curl -sLD - ${s.url}/invalid-redirect`, (error, stdout) => {
console.log('cURL ERR\n', error)
console.log('cURL OUT\n', stdout);
});
await t.throwsAsync(() => got(`${s.url}/invalid-redirect`));
}); |
As per below example domain redirects
302
to invalid host name emptyhttp://
this breaksgot
and throwsUnhandledPromiseRejectionWarning
regardless of any attempt tocatch
errors.The text was updated successfully, but these errors were encountered: