Skip to content

Commit

Permalink
Merge pull request #39 from floatdrop/master
Browse files Browse the repository at this point in the history
Improve error message on not 2xx code
  • Loading branch information
sindresorhus committed Feb 6, 2015
2 parents b13f842 + ef8bdeb commit c968d5b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Expand Up @@ -11,6 +11,7 @@ var read = require('read-all-stream');
var timeout = require('timed-out');
var prependHttp = require('prepend-http');
var lowercaseKeys = require('lowercase-keys');
var status = require('statuses');

function got(url, opts, cb) {
if (typeof opts === 'function') {
Expand Down Expand Up @@ -65,7 +66,7 @@ function got(url, opts, cb) {
var res = response;

// redirect
if (statusCode >= 300 && statusCode < 400 && 'location' in res.headers) {
if (status.redirect[statusCode] && 'location' in res.headers) {
res.resume(); // Discard response

if (++redirectCount > 10) {
Expand All @@ -85,7 +86,7 @@ function got(url, opts, cb) {

if (statusCode < 200 || statusCode > 299) {
read(res, encoding, function (error, data) {
var err = error || new Error('Couldn\'t connect to ' + url + '.');
var err = error || new Error(url + ' response code is ' + statusCode + ' (' + status[statusCode] + ')');
err.code = statusCode;
cb(err, data, response);
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -45,6 +45,7 @@
"object-assign": "^2.0.0",
"prepend-http": "^1.0.0",
"read-all-stream": "^1.0.0",
"statuses": "^1.2.1",
"timed-out": "^2.0.0"
},
"devDependencies": {
Expand Down
29 changes: 29 additions & 0 deletions test/test-error.js
@@ -0,0 +1,29 @@
'use strict';
var tape = require('tape');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();

s.on('/', function (req, res) {
res.statusCode = 404;
res.end('not');
});

tape('setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});

tape('error message', function (t) {
got(s.url, function (err) {
t.ok(err);
t.equal(err.message, 'http://localhost:6767 response code is 404 (Not Found)');
t.end();
});
});

tape('cleanup', function (t) {
s.close();
t.end();
});

0 comments on commit c968d5b

Please sign in to comment.