Skip to content

Commit

Permalink
Add optional statusCode parameter to Boom.isBoom, resolves #265 (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fcmam5 authored Mar 12, 2020
1 parent 6ffa406 commit 7e4dcd2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
10 changes: 8 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ var error = new Error('Unexpected input');
Boom.boomify(error, { statusCode: 400 });
```

##### `isBoom(err)`
##### `isBoom(err, statusCode)`

Identifies whether an error is a `Boom` object. Same as calling `instanceof Boom`.
Identifies whether an error is a `Boom` object. Same as calling `instanceof Boom`.
- `err` - Error object.
- `statusCode` - optional status code.

```js
Boom.isBoom(Boom.badRequest()); // true
Boom.isBoom(Boom.badRequest(), 400); // true
```
#### HTTP 4xx Errors

##### `Boom.badRequest([message], [data])`
Expand Down
5 changes: 3 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ export interface Output {
* Specifies if an error object is a valid boom object
*
* @param err - The error object
* @param statusCode - Optional status code
*
* @returns Returns a boolean stating if the error object is a valid boom object
* @returns Returns a boolean stating if the error object is a valid boom object and it has the provided statusCode (if present)
*/
export function isBoom(err: Error): err is Boom;
export function isBoom(err: Error, statusCode?: number): err is Boom;


/**
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ exports.Boom = class extends Error {
};


exports.isBoom = function (err) {
exports.isBoom = function (err, statusCode) {

return err instanceof Error && !!err.isBoom;
return err instanceof Error && !!err.isBoom && (statusCode ? err.output.statusCode === statusCode : true);
};


Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ describe('Boom', () => {
expect(Boom.isBoom({ isBoom: true })).to.be.false();
expect(Boom.isBoom(null)).to.be.false();
});

it('returns true for valid boom object and valid status code', () => {

expect(Boom.isBoom(Boom.notFound(),404)).to.be.true();
});

it('returns false for valid boom object and wrong status code', () => {

expect(Boom.isBoom(Boom.notFound(),503)).to.be.false();
});
});

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

0 comments on commit 7e4dcd2

Please sign in to comment.