diff --git a/API.md b/API.md index 350c93c..4102b2c 100755 --- a/API.md +++ b/API.md @@ -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])` diff --git a/lib/index.d.ts b/lib/index.d.ts index 6e6b0cd..b5f9dd4 100755 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -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; /** diff --git a/lib/index.js b/lib/index.js index 166e10e..359e877 100755 --- a/lib/index.js +++ b/lib/index.js @@ -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); }; diff --git a/test/index.js b/test/index.js index b7c8c9f..dcea4eb 100755 --- a/test/index.js +++ b/test/index.js @@ -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()', () => {