Skip to content

Commit

Permalink
Boom.boomify(). Closes #160
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 18, 2017
1 parent 8d34e75 commit 8b35a4c
Show file tree
Hide file tree
Showing 5 changed files with 739 additions and 608 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ dump.rdb
node_modules
results.tap
results.xml
npm-shrinkwrap.json
config.json
.DS_Store
*/.DS_Store
Expand All @@ -14,5 +13,6 @@ config.json
*/._*
*/*/._*
coverage.*
lib-cov
.settings
package-lock.json

23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Lead Maintainer: [Adam Bretz](https://github.com/arb)

- [Boom](#boom)
- [Helper Methods](#helper-methods)
- [`boomify(error, [options])`](#boomifyerror-options)
- [`wrap(error, [statusCode], [message])`](#wraperror-statuscode-message)
- [`create(statusCode, [message], [data])`](#createstatuscode-message-data)
- [HTTP 4xx Errors](#http-4xx-errors)
Expand Down Expand Up @@ -73,8 +74,30 @@ The `Boom` object also supports the following method:

## Helper Methods

### `boomify(error, [options])`

Decorates an error with the **boom** properties where:
- `error` - the `Error` object to decorate.
- `options` - optional object with the following optional settings:
- `statusCode` - the HTTP status code. Defaults to `500` if no status code is already set.
- `message` - error message string. If the error already has a message, the provided `message` is added as a prefix.
Defaults to no message.
- `override` - if `false`, the `error` provided is a **boom** object, and a `statusCode` or `message` are provided,
the values are ignored. Defaults to `true` (apply the provided `statusCode` and `message` options to the error
regardless of its type, `Error` or **boom** object).

Note: This method replaces the [`wrap()`](#wraperror-statuscode-message) and changes the default behavior to override
existing **boom** objects with the provided status code and message.

```js
var error = new Error('Unexpected input');
Boom.boomify(error, { statusCode: 400 });
```

### `wrap(error, [statusCode], [message])`

Note: This method is deprecated.

Decorates an error with the **boom** properties where:
- `error` - the error object to wrap. If `error` is already a **boom** object, returns back the same object.
- `statusCode` - optional HTTP status code. Defaults to `500`.
Expand Down
20 changes: 20 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ const internals = {
};


exports.boomify = function (error, options) {

Hoek.assert(error instanceof Error, 'Cannot wrap non-Error object');

options = options || {};

if (!error.isBoom) {
return internals.initialize(error, options.statusCode || 500, options.message);
}

if (options.override === false || // Defaults to true
(!options.statusCode && !options.message)) {

return error;
}

return internals.initialize(error, options.statusCode || error.output.statusCode, options.message);
};


exports.wrap = function (error, statusCode, message) {

Hoek.assert(error instanceof Error, 'Cannot wrap non-Error object');
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "boom",
"description": "HTTP-friendly error objects",
"version": "5.1.0",
"version": "5.2.0",
"repository": "git://github.com/hapijs/boom",
"main": "lib/index.js",
"keywords": [
Expand All @@ -16,7 +16,7 @@
},
"devDependencies": {
"code": "4.x.x",
"lab": "13.x.x",
"lab": "14.x.x",
"markdown-toc": "0.12.x"
},
"scripts": {
Expand Down
Loading

0 comments on commit 8b35a4c

Please sign in to comment.