Skip to content

Commit

Permalink
Return an error object on connection failure
Browse files Browse the repository at this point in the history
Fixes #9.
  • Loading branch information
kevva committed Oct 17, 2014
1 parent 51b1b90 commit 4a6b6d7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ module.exports = function (url, opts, cb) {

if (res.statusCode < 200 || res.statusCode > 299) {
res.destroy();
cb(res.statusCode);
var err = new Error('Couldn\'t connect to ' + url + '.');
err.code = res.statusCode;
cb(err);
return;
}

Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ it('should do HTTPS request', function (done) {
});
});

it('should should return status code as error when not 200', function (done) {
it('should should return status code as error code when not 200', function (done) {
got('http://sindresorhus.com/sfsadfasdfadsga', function (err, data) {
assert.strictEqual(err, 404);
assert.strictEqual(err.code, 404);
done();
});
});
Expand Down Expand Up @@ -84,7 +84,7 @@ it('should proxy errors to the stream', function (done) {
var stream = got('http://sindresorhus.com/sfsadfasdfadsga');

stream.on('error', function (error) {
assert.strictEqual(error, 404);
assert.strictEqual(error.code, 404);
done();
});
});

0 comments on commit 4a6b6d7

Please sign in to comment.