Skip to content

Commit

Permalink
Catch more errors (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Jul 5, 2018
1 parent 058452b commit f621184
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
19 changes: 16 additions & 3 deletions index.js
Expand Up @@ -144,6 +144,13 @@ function requestAsEventEmitter(opts) {

redirectUrl = urlLib.resolve(urlLib.format(opts), bufferString);

try {
decodeURI(redirectUrl);
} catch (err) {
ee.emit('error', err);
return;
}

redirects.push(redirectUrl);

const redirectOpts = {
Expand All @@ -161,8 +168,8 @@ function requestAsEventEmitter(opts) {
setImmediate(() => {
try {
getResponse(res, opts, ee, redirects);
} catch (e) {
ee.emit('error', e);
} catch (err) {
ee.emit('error', err);
}
});
});
Expand Down Expand Up @@ -191,7 +198,13 @@ function requestAsEventEmitter(opts) {
const backoff = opts.retries(++retryCount, err);

if (backoff) {
setTimeout(get, backoff, opts);
setTimeout(opts => {
try {
get(opts);
} catch (e) {
ee.emit('error', e);
}
}, backoff, opts);
return;
}

Expand Down
12 changes: 12 additions & 0 deletions test/redirects.js
Expand Up @@ -123,6 +123,13 @@ test.before('setup', async () => {
res.end();
});

http.on('/malformedRedirect', (req, res) => {
res.writeHead(302, {
location: '/%D8'
});
res.end();
});

await http.listen(http.port);
await https.listen(https.port);
});
Expand Down Expand Up @@ -210,3 +217,8 @@ test('redirect response contains old url', async t => {
test('redirect response contains utf8 with binary encoding', async t => {
t.is((await got(`${http.url}/redirect-with-utf8-binary`)).body, 'reached');
});

test('throws on malformed redirect URI', async t => {
const err = await t.throws(got(`${http.url}/malformedRedirect`));
t.is(err.name, 'URIError');
});

0 comments on commit f621184

Please sign in to comment.