Skip to content
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

Handle errors thrown synchronously by http.request(). #210

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,41 @@ function requestAsEventEmitter(opts) {

const get = opts => {
const fn = opts.protocol === 'https:' ? https : http;
let req;

const req = fn.request(opts, res => {
const statusCode = res.statusCode;
try {
req = fn.request(opts, res => {
const statusCode = res.statusCode;

if (redirectUrl) {
res.url = redirectUrl;
}
if (redirectUrl) {
res.url = redirectUrl;
}

if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
res.resume();
if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
res.resume();

if (++redirectCount > 10) {
ee.emit('error', new got.MaxRedirectsError(statusCode, opts), null, res);
return;
}
if (++redirectCount > 10) {
ee.emit('error', new got.MaxRedirectsError(statusCode, opts), null, res);
return;
}

redirectUrl = urlLib.resolve(urlLib.format(opts), res.headers.location);
const redirectOpts = Object.assign({}, opts, urlLib.parse(redirectUrl));
redirectUrl = urlLib.resolve(urlLib.format(opts), res.headers.location);
const redirectOpts = Object.assign({}, opts, urlLib.parse(redirectUrl));

ee.emit('redirect', res, redirectOpts);
ee.emit('redirect', res, redirectOpts);

get(redirectOpts);
get(redirectOpts);

return;
}
return;
}

setImmediate(() => {
ee.emit('response', typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res);
setImmediate(() => {
ee.emit('response', typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res);
});
});
});
} catch (err) {
return ee.emit('error', new got.RequestError(err, opts));
}

req.once('error', err => {
const backoff = opts.retries(++retryCount, err);
Expand Down
9 changes: 9 additions & 0 deletions test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ test('options.body error message', async t => {
}
});

test('invalid url', async t => {
try {
await got('htttp://google.com');
t.fail('Exception was not thrown');
} catch (err) {
t.regex(err.message, /Protocol .+ not supported/);
}
});

test.after('cleanup', async () => {
await s.close();
});