Skip to content

Commit

Permalink
Only create boom errors for shortcut responses
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Mar 23, 2017
1 parent 901d521 commit 34d4ed0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Initiate an HTTP request.
to force SSL version 3. The possible values depend on your installation of OpenSSL. Read the official OpenSSL docs
for possible [SSL_METHODS](http://www.openssl.org/docs/ssl/ssl.html#DEALING_WITH_PROTOCOL_METHODS).
- `callback` - The optional callback function using the signature `function (err, response)` where:
- `err` - Any error that may have occurred during the handling of the request or a Boom error object if the response has an error status code.
- `err` - Any error that may have occurred during the handling of the request.
- `response` - The [HTTP Incoming Message](http://nodejs.org/api/http.html#http_http_incomingmessage)
object, which is also a readable stream.

Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,6 @@ internals.Client.prototype.request = function (method, url, options, callback, _
req.abort();
}

if (!err && res.statusCode >= 400) {
err = Boom.create(res.statusCode, res.statusMessage);
}

req.removeListener('response', onResponse);
req.removeListener('error', onError);
req.on('error', Hoek.ignore);
Expand Down Expand Up @@ -524,6 +520,10 @@ internals.Client.prototype._shortcut = function (method, uri, options, callback)

this.read(res, options, (err, payload) => {

if (!err && res.statusCode >= 400) {
return callback(Boom.create(res.statusCode));
}

return callback(err, res, payload);
});
});
Expand Down
46 changes: 23 additions & 23 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1062,27 +1062,6 @@ describe('request()', () => {
server.listen(0);
});

it('handles error responses with a boom error object', (done) => {

const server = Http.createServer((req, res) => {

res.writeHead(400);
res.end();
});

server.once('listening', () => {

Wreck.request('get', 'http://127.0.0.1:' + server.address().port, { payload: '' }, (err) => {

expect(err.isBoom).to.be.true();
expect(err.message).to.equal('Bad Request');
done();
});
});

server.listen(0);
});

it('handles request errors with a boom response when payload is being sent', (done) => {

const server = Http.createServer((req, res) => {
Expand Down Expand Up @@ -1428,8 +1407,7 @@ describe('request()', () => {

Wreck.request('post', 'http://localhost:' + server.address().port, { headers: { connection: 'close' }, payload: null }, (err, res) => {

expect(err).exist();
expect(err.isBoom).to.be.true();
expect(err).to.not.exist();
Wreck.read(res, null, (err, body) => {

expect(err).to.not.exist();
Expand Down Expand Up @@ -2070,6 +2048,28 @@ describe('Shortcut', () => {
});
});
});

it('handles error responses with a boom error object', (done) => {

const server = Http.createServer((req, res) => {

res.writeHead(400);
res.end();
});

server.once('listening', () => {

Wreck.get('http://127.0.0.1:' + server.address().port, (err) => {

expect(err.isBoom).to.be.true();
expect(err.message).to.equal('Bad Request');
done();
});
});

server.listen(0);
});

});

describe('json', () => {
Expand Down

0 comments on commit 34d4ed0

Please sign in to comment.