Skip to content

Commit

Permalink
Explicit Error classes
Browse files Browse the repository at this point in the history
Closes #83 and closes #86
  • Loading branch information
floatdrop committed Jul 24, 2015
1 parent e01aa05 commit 11680b1
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 56 deletions.
69 changes: 41 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var EventEmitter = require('events').EventEmitter;
var http = require('http');
var https = require('https');
var urlLib = require('url');
var util = require('util');
var querystring = require('querystring');
var objectAssign = require('object-assign');
var duplexify = require('duplexify');
Expand All @@ -13,17 +12,9 @@ var timedOut = require('timed-out');
var prependHttp = require('prepend-http');
var lowercaseKeys = require('lowercase-keys');
var isRedirect = require('is-redirect');
var NestedErrorStacks = require('nested-error-stacks');
var pinkiePromise = require('pinkie-promise');
var unzipResponse = require('unzip-response');

function GotError(message, nested) {
NestedErrorStacks.call(this, message, nested);
objectAssign(this, nested, {nested: this.nested});
}

util.inherits(GotError, NestedErrorStacks);
GotError.prototype.name = 'GotError';
var createErrorClass = require('create-error-class');

function requestAsEventEmitter(opts) {
opts = opts || {};
Expand All @@ -33,19 +24,18 @@ function requestAsEventEmitter(opts) {

var get = function (opts) {
var fn = opts.protocol === 'https:' ? https : http;
var url = urlLib.format(opts);

var req = fn.request(opts, function (res) {
var statusCode = res.statusCode;
if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
res.resume();

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

var redirectUrl = urlLib.resolve(url, res.headers.location);
var redirectUrl = urlLib.resolve(urlLib.format(opts), res.headers.location);
var redirectOpts = objectAssign({}, opts, urlLib.parse(redirectUrl));

ee.emit('redirect', res, redirectOpts);
Expand All @@ -56,7 +46,7 @@ function requestAsEventEmitter(opts) {

ee.emit('response', unzipResponse(res));
}).once('error', function (err) {
ee.emit('error', new GotError('Request to ' + url + ' failed', err));
ee.emit('error', new got.RequestError(err, opts));
});

if (opts.timeout) {
Expand All @@ -72,7 +62,6 @@ function requestAsEventEmitter(opts) {

function asCallback(opts, cb) {
var ee = requestAsEventEmitter(opts);
var url = urlLib.format(opts);

ee.on('request', function (req) {
if (isStream.readable(opts.body)) {
Expand All @@ -87,22 +76,21 @@ function asCallback(opts, cb) {
ee.on('response', function (res) {
readAllStream(res, opts.encoding, function (err, data) {
if (err) {
cb(new GotError('Reading ' + url + ' response failed', err), null, res);
cb(new got.ReadError(err, opts), null, res);
return;
}

var statusCode = res.statusCode;

if (statusCode < 200 || statusCode > 299) {
err = new GotError(opts.method + ' ' + url + ' response code is ' + statusCode + ' (' + http.STATUS_CODES[statusCode] + ')', err);
err.code = statusCode;
err = new got.HTTPError(statusCode, opts);
}

if (opts.json && statusCode !== 204) {
try {
data = JSON.parse(data);
} catch (e) {
err = new GotError('Parsing ' + url + ' response failed', new GotError(e.message, err));
err = new got.ParseError(e, opts);
}
}

Expand Down Expand Up @@ -135,7 +123,7 @@ function asStream(opts) {
var proxy = duplexify();

if (opts.json) {
throw new GotError('got can not be used as stream when options.json is used');
throw new Error('got can not be used as stream when options.json is used');
}

if (opts.body) {
Expand Down Expand Up @@ -179,11 +167,11 @@ function asStream(opts) {

function normalizeArguments(url, opts) {
if (typeof url !== 'string' && typeof url !== 'object') {
throw new GotError('Parameter `url` must be a string or object, not ' + typeof url);
throw new Error('Parameter `url` must be a string or object, not ' + typeof url);
}

opts = objectAssign(
{protocol: 'http:'},
{protocol: 'http:', path: ''},
typeof url === 'string' ? urlLib.parse(prependHttp(url)) : url,
opts
);
Expand All @@ -193,17 +181,13 @@ function normalizeArguments(url, opts) {
'accept-encoding': 'gzip,deflate'
}, lowercaseKeys(opts.headers));

if (opts.pathname) {
opts.path = opts.pathname;
}

var query = opts.query;
if (query) {
if (typeof query !== 'string') {
opts.query = querystring.stringify(query);
}

opts.path = opts.pathname + '?' + opts.query;
opts.path = opts.path.split('?')[0] + '?' + opts.query;
delete opts.query;
}

Expand All @@ -214,7 +198,7 @@ function normalizeArguments(url, opts) {
var body = opts.body;
if (body) {
if (typeof body !== 'string' && !Buffer.isBuffer(body) && !isStream.readable(body)) {
throw new GotError('options.body must be a ReadableStream, string or Buffer');
throw new Error('options.body must be a ReadableStream, string or Buffer');
}

opts.method = opts.method || 'POST';
Expand Down Expand Up @@ -276,4 +260,33 @@ helpers.forEach(function (el) {
};
});

function stdError(error, opts) {
objectAssign(this, {
message: error.message,
code: error.code,
host: opts.host,
hostname: opts.hostname,
method: opts.method,
path: opts.path
});
}

got.RequestError = createErrorClass('RequestError', stdError);
got.ReadError = createErrorClass('ReadError', stdError);
got.ParseError = createErrorClass('ParseError', stdError);

got.HTTPError = createErrorClass('HTTPError', function (statusCode, opts) {
stdError.call(this, {}, opts);
this.statusCode = statusCode;
this.statusMessage = http.STATUS_CODES[this.statusCode];
this.message = 'Response code ' + this.statusCode + ' (' + this.statusMessage + ')';
});

got.MaxRedirectsError = createErrorClass('MaxRedirectsError', function (statusCode, opts) {
stdError.call(this, {}, opts);
this.statusCode = statusCode;
this.statusMessage = http.STATUS_CODES[this.statusCode];
this.message = 'Redirected 10 times. Aborting.';
});

module.exports = got;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
"fetch"
],
"dependencies": {
"create-error-class": "^2.0.0",
"duplexify": "^3.2.0",
"is-redirect": "^1.0.0",
"is-stream": "^1.0.0",
"lowercase-keys": "^1.0.0",
"nested-error-stacks": "^1.0.0",
"object-assign": "^3.0.0",
"pinkie-promise": "^1.0.0",
"prepend-http": "^1.0.0",
Expand Down
25 changes: 23 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,29 @@ When in stream mode, you can listen for events:

Sets `options.method` to the method name and makes a request.

## Errors

Each Error contains (if available) `host`, `hostname`, `method` and `path` properties to make debug easier.

#### got.RequestError

Happens, when making request failed. Should contain `code` property with error class code (like `ECONNREFUSED`).

#### got.ReadError

Happens, when reading from response stream failed.

#### got.ParseError

Happens, when `json` option is enabled and `JSON.parse` failed.

#### got.HTTPError

Happens, when server response code is not 2xx. Contains `statusCode` and `statusMessage`.

#### got.MaxRedirectsError

Happens, when server redirects you more than 10 times.

## Proxy

Expand Down Expand Up @@ -201,8 +224,6 @@ This should only ever be done if you have Node version 0.10.x and at the top-lev
## Related

- [gh-got](https://github.com/sindresorhus/gh-got) - Convenience wrapper for interacting with the GitHub API
- [got-promise](https://github.com/floatdrop/got-promise) - Promise wrapper


## Created by

Expand Down
7 changes: 0 additions & 7 deletions test/test-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ test('overrides querystring from opts', function (t) {
});
});

test('pathname confusion', function (t) {
got({protocol: 'http:', hostname: s.host, port: s.port, pathname: '/test'}, function (err) {
t.error(err);
t.end();
});
});

test('cleanup', function (t) {
s.close();
t.end();
Expand Down
10 changes: 6 additions & 4 deletions test/test-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ test('setup', function (t) {
test('error message', function (t) {
got(s.url, function (err) {
t.ok(err);
t.equal(err.message, 'GET http://localhost:6767/ response code is 404 (Not Found)');
t.equal(err.message, 'Response code 404 (Not Found)');
t.equal(err.host, 'localhost:6767');
t.equal(err.method, 'GET');
t.end();
});
});

test('dns error message', function (t) {
got('.com', function (err) {
t.ok(err);
t.equal(err.message, 'Request to http://.com/ failed');
t.ok(err.nested);
t.ok(/getaddrinfo ENOTFOUND/.test(err.nested.message));
t.ok(/getaddrinfo ENOTFOUND/.test(err.message));
t.equal(err.host, '.com');
t.equal(err.method, 'GET');
t.end();
});
});
Expand Down
4 changes: 3 additions & 1 deletion test/test-gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ test('ungzip content', function (t) {
test('ungzip error', function (t) {
got(s.url + '/corrupted', function (err) {
t.ok(err);
t.equal(err.message, 'Reading ' + s.url + '/corrupted response failed');
t.equal(err.message, 'incorrect header check');
t.equal(err.path, '/corrupted');
t.equal(err.name, 'ReadError');
t.end();
});
});
Expand Down
1 change: 0 additions & 1 deletion test/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ test('promise mode', function (t) {

got.get(s.url + '/404')
.catch(function (err) {
t.equal(err.message, 'GET http://localhost:6767/404 response code is 404 (Not Found)');
t.equal(err.response.body, 'not found');
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/test-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('empty response', function (t) {
test('error with code', function (t) {
got(s.url + '/404', function (err, data) {
t.ok(err);
t.equal(err.code, 404);
t.equal(err.statusCode, 404);
t.equal(data, 'not');
t.end();
});
Expand Down
12 changes: 4 additions & 8 deletions test/test-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ test('json option should not parse responses without a body', function (t) {
test('json option wrap parsing errors', function (t) {
got(s.url + '/invalid', {json: true}, function (err) {
t.ok(err);
t.equal(err.message, 'Parsing ' + s.url + '/invalid response failed');
t.ok(err.nested);
t.equal(err.nested.message, 'Unexpected token /');
t.equal(err.message, 'Unexpected token /');
t.equal(err.path, '/invalid');
t.end();
});
});
Expand All @@ -70,11 +69,8 @@ test('json option should catch errors on invalid non-200 responses', function (t
got(s.url + '/non200-invalid', {json: true}, function (err, json) {
t.ok(err);
t.deepEqual(json, 'Internal error');
t.equal(err.message, 'Parsing http://localhost:6767/non200-invalid response failed');
t.ok(err.nested);
t.equal(err.nested.message, 'Unexpected token I');
t.ok(err.nested.nested);
t.equal(err.nested.nested.message, 'GET http://localhost:6767/non200-invalid response code is 500 (Internal Server Error)');
t.equal(err.message, 'Unexpected token I');
t.equal(err.path, '/non200-invalid');
t.end();
});
});
Expand Down
5 changes: 3 additions & 2 deletions test/test-redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ test('hostname+path in options are not breaking redirects', function (t) {

test('redirect only GET and HEAD requests', function (t) {
got(s.url + '/relative', {body: 'wow'}, function (err) {
t.equal(err.message, 'POST http://localhost:6767/relative response code is 302 (Moved Temporarily)');
t.equal(err.code, 302);
t.equal(err.message, 'Response code 302 (Moved Temporarily)');
t.equal(err.path, '/relative');
t.equal(err.statusCode, 302);
t.end();
});
});
Expand Down
1 change: 0 additions & 1 deletion test/test-stream.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var test = require('tap').test;
var from2Array = require('from2-array');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();
Expand Down

0 comments on commit 11680b1

Please sign in to comment.