Browse Source

Return an error object on connection failure

Fixes #9.
http2
Kevin Martensson 10 years ago
parent
commit
4a6b6d7cc7
  1. 4
      index.js
  2. 6
      test.js

4
index.js

@ -61,7 +61,9 @@ module.exports = function (url, opts, cb) {
if (res.statusCode < 200 || res.statusCode > 299) { if (res.statusCode < 200 || res.statusCode > 299) {
res.destroy(); res.destroy();
cb(res.statusCode); var err = new Error('Couldn\'t connect to ' + url + '.');
err.code = res.statusCode;
cb(err);
return; return;
} }

6
test.js

@ -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) { got('http://sindresorhus.com/sfsadfasdfadsga', function (err, data) {
assert.strictEqual(err, 404); assert.strictEqual(err.code, 404);
done(); done();
}); });
}); });
@ -84,7 +84,7 @@ it('should proxy errors to the stream', function (done) {
var stream = got('http://sindresorhus.com/sfsadfasdfadsga'); var stream = got('http://sindresorhus.com/sfsadfasdfadsga');
stream.on('error', function (error) { stream.on('error', function (error) {
assert.strictEqual(error, 404); assert.strictEqual(error.code, 404);
done(); done();
}); });
}); });

Loading…
Cancel
Save