Skip to content

Commit 7e4dcd2

Browse files
authored
Add optional statusCode parameter to Boom.isBoom, resolves #265 (#266)
1 parent 6ffa406 commit 7e4dcd2

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

API.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,16 @@ var error = new Error('Unexpected input');
6767
Boom.boomify(error, { statusCode: 400 });
6868
```
6969

70-
##### `isBoom(err)`
70+
##### `isBoom(err, statusCode)`
7171

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

76+
```js
77+
Boom.isBoom(Boom.badRequest()); // true
78+
Boom.isBoom(Boom.badRequest(), 400); // true
79+
```
7480
#### HTTP 4xx Errors
7581

7682
##### `Boom.badRequest([message], [data])`

lib/index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,11 @@ export interface Output {
130130
* Specifies if an error object is a valid boom object
131131
*
132132
* @param err - The error object
133+
* @param statusCode - Optional status code
133134
*
134-
* @returns Returns a boolean stating if the error object is a valid boom object
135+
* @returns Returns a boolean stating if the error object is a valid boom object and it has the provided statusCode (if present)
135136
*/
136-
export function isBoom(err: Error): err is Boom;
137+
export function isBoom(err: Error, statusCode?: number): err is Boom;
137138

138139

139140
/**

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ exports.Boom = class extends Error {
9696
};
9797

9898

99-
exports.isBoom = function (err) {
99+
exports.isBoom = function (err, statusCode) {
100100

101-
return err instanceof Error && !!err.isBoom;
101+
return err instanceof Error && !!err.isBoom && (statusCode ? err.output.statusCode === statusCode : true);
102102
};
103103

104104

test/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ describe('Boom', () => {
145145
expect(Boom.isBoom({ isBoom: true })).to.be.false();
146146
expect(Boom.isBoom(null)).to.be.false();
147147
});
148+
149+
it('returns true for valid boom object and valid status code', () => {
150+
151+
expect(Boom.isBoom(Boom.notFound(),404)).to.be.true();
152+
});
153+
154+
it('returns false for valid boom object and wrong status code', () => {
155+
156+
expect(Boom.isBoom(Boom.notFound(),503)).to.be.false();
157+
});
148158
});
149159

150160
describe('boomify()', () => {

0 commit comments

Comments
 (0)