Skip to content

Commit

Permalink
Merge pull request #158 from geek/82
Browse files Browse the repository at this point in the history
Wrap error response with Boom
  • Loading branch information
geek committed Mar 21, 2017
2 parents 961d678 + d4ff006 commit 5827bb8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
12 changes: 6 additions & 6 deletions 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.
- `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.
- `response` - The [HTTP Incoming Message](http://nodejs.org/api/http.html#http_http_incomingmessage)
object, which is also a readable stream.

Expand Down Expand Up @@ -147,7 +147,7 @@ Convenience method for GET operations.
- `options` - Optional config object containing settings for both `request` and
`read` operations.
- `callback` - The callback function using the signature `function (err, response, payload)` where:
- `err` - Any error that may have occurred during handling of the request.
- `err` - Any error that may have occurred during handling of the request or a Boom error object if the response has an error status code.
- `response` - The [HTTP Incoming Message](http://nodejs.org/api/http.html#http_http_incomingmessage)
object, which is a readable stream that has "ended" and contains no more data to read.
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).
Expand All @@ -162,7 +162,7 @@ Convenience method for POST operations.
- `options` - Optional config object containing settings for both `request` and
`read` operations.
- `callback` - The callback function using the signature `function (err, response, payload)` where:
- `err` - Any error that may have occurred during handling of the request.
- `err` - Any error that may have occurred during handling of the request or a Boom error object if the response has an error status code.
- `response` - The [HTTP Incoming Message](http://nodejs.org/api/http.html#http_http_incomingmessage)
object, which is a readable stream that has "ended" and contains no more data to read.
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).
Expand All @@ -176,7 +176,7 @@ Convenience method for PATCH operations.
- `options` - Optional config object containing settings for both `request` and
`read` operations.
- `callback` - The callback function using the signature `function (err, response, payload)` where:
- `err` - Any error that may have occurred during handling of the request.
- `err` - Any error that may have occurred during handling of the request or a Boom error object if the response has an error status code.
- `response` - The [HTTP Incoming Message](http://nodejs.org/api/http.html#http_http_incomingmessage)
object, which is a readable stream that has "ended" and contains no more data to read.
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).
Expand All @@ -191,7 +191,7 @@ Convenience method for PUT operations.
- `options` - Optional config object containing settings for both `request` and
`read` operations.
- `callback` - The callback function using the signature `function (err, response, payload)` where:
- `err` - Any error that may have occurred during handling of the request.
- `err` - Any error that may have occurred during handling of the request or a Boom error object if the response has an error status code.
- `response` - The [HTTP Incoming Message](http://nodejs.org/api/http.html#http_http_incomingmessage)
object, which is a readable stream that has "ended" and contains no more data to read.
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).
Expand All @@ -206,7 +206,7 @@ Convenience method for DELETE operations.
- `options` - Optional config object containing settings for both `request` and
`read` operations.
- `callback` - The callback function using the signature `function (err, response, payload)` where:
- `err` - Any error that may have occurred during handling of the request.
- `err` - Any error that may have occurred during handling of the request or a Boom error object if the response has an error status code.
- `response` - The [HTTP Incoming Message](http://nodejs.org/api/http.html#http_http_incomingmessage)
object, which is a readable stream that has "ended" and contains no more data to read.
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).
Expand Down
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ internals.Client.prototype.request = function (method, url, options, callback, _

const finish = (err, res) => {

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

if (err) {
req.abort();
}
Expand Down
24 changes: 23 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,27 @@ 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 @@ -1368,7 +1389,8 @@ describe('request()', () => {

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

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

expect(err).to.not.exist();
Expand Down

0 comments on commit 5827bb8

Please sign in to comment.