Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite11f87d

Browse files
committed
Add isHttpError export to determine if value is an HTTP error
closes#56
1 parentd32bcc4 commite11f87d

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

‎HISTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Add`isHttpError` export to determine if value is an HTTP error
45
* deps: setprototypeof@1.2.0
56

67
2019-06-24 / 1.7.3

‎README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ fs.readFile('foo.txt', function (err, buf) {
8888
-`error` - the error object to extend
8989
-`properties` - custom properties to attach to the object
9090

91+
###createError.isHttpError(val)
92+
93+
Determine if the provided`val` is an`HttpError`. This will return`true`
94+
if the error inherits from the`HttpError` constructor of this module or
95+
matches the "duck type" for an error this module creates. All outputs from
96+
the`createError` factory will return`true` for this function, including
97+
if an non-`HttpError` was passed into the factory.
98+
9199
###new createError\[code || name\](\[msg]\))
92100

93101
Create a new error object with the given message`msg`.

‎index.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var toIdentifier = require('toidentifier')
2525

2626
module.exports=createError
2727
module.exports.HttpError=createHttpErrorConstructor()
28+
module.exports.isHttpError=createIsHttpErrorFunction(module.exports.HttpError)
2829

2930
// Populate exports for all constructors
3031
populateConstructorExports(module.exports,statuses.codes,module.exports.HttpError)
@@ -172,6 +173,27 @@ function createClientErrorConstructor (HttpError, name, code) {
172173
returnClientError
173174
}
174175

176+
/**
177+
* Create function to test is a value is a HttpError.
178+
*@private
179+
*/
180+
181+
functioncreateIsHttpErrorFunction(HttpError){
182+
returnfunctionisHttpError(val){
183+
if(!val||typeofval!=='object'){
184+
returnfalse
185+
}
186+
187+
if(valinstanceofHttpError){
188+
returntrue
189+
}
190+
191+
returnvalinstanceofError&&
192+
typeofval.expose==='boolean'&&
193+
typeofval.statusCode==='number'&&val.status===val.statusCode
194+
}
195+
}
196+
175197
/**
176198
* Create a constructor for a server error.
177199
*@private

‎test/test.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,62 @@ describe('createError(status, message)', function () {
122122
})
123123
})
124124

125+
describe('createError.isHttpError(val)',function(){
126+
describe('when val is undefined',function(){
127+
it('should return false',function(){
128+
assert.strictEqual(createError.isHttpError(undefined),false)
129+
})
130+
})
131+
132+
describe('when val is null',function(){
133+
it('should return false',function(){
134+
assert.strictEqual(createError.isHttpError(null),false)
135+
})
136+
})
137+
138+
describe('when val is a number',function(){
139+
it('should return false',function(){
140+
assert.strictEqual(createError.isHttpError(42),false)
141+
})
142+
})
143+
144+
describe('when val is a string',function(){
145+
it('should return false',function(){
146+
assert.strictEqual(createError.isHttpError('foobar'),false)
147+
})
148+
})
149+
150+
describe('when val is an empty object',function(){
151+
it('should return false',function(){
152+
assert.strictEqual(createError.isHttpError({}),false)
153+
})
154+
})
155+
156+
describe('when val is a plain Error',function(){
157+
it('should return false',function(){
158+
assert.strictEqual(createError.isHttpError(newError()),false)
159+
})
160+
})
161+
162+
describe('when val is an instance of HttpError',function(){
163+
it('should return true',function(){
164+
varerr=createError(500)
165+
166+
assert.ok(errinstanceofcreateError.HttpError)
167+
assert.strictEqual(createError.isHttpError(err),true)
168+
})
169+
})
170+
171+
describe('when val is an Error passed to createError',function(){
172+
it('should return true',function(){
173+
varerr=createError(500,newError())
174+
175+
assert.ok(!(errinstanceofcreateError.HttpError))
176+
assert.strictEqual(createError.isHttpError(err),true)
177+
})
178+
})
179+
})
180+
125181
describe('HTTP Errors',function(){
126182
it('createError(status, props)',function(){
127183
varerr=createError(404,{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp