Skip to content

Commit c05055f

Browse files
committed
close #98
1 parent 9337a91 commit c05055f

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ class API {
258258

259259
let message
260260

261+
// Set the status code
262+
response.status(code ? code : this._errorStatus)
263+
261264
let info = {
262265
detail,
263266
statusCode: response._statusCode,
@@ -266,13 +269,11 @@ class API {
266269
}
267270

268271
if (e instanceof Error) {
269-
response.status(code ? code : this._errorStatus)
270272
message = e.message
271-
this.log.fatal(message, info)
273+
this.log.fatal(message,info)
272274
} else {
273-
response.status(code)
274275
message = e
275-
this.log.error(message, info)
276+
this.log.error(message,info)
276277
}
277278

278279
// If first time through, process error middleware
@@ -323,6 +324,9 @@ class API {
323324
console.log(JSON.stringify(this._logger.format(access,response._request,response))) // eslint-disable-line no-console
324325
}
325326

327+
// Reset global error code
328+
this._errorStatus = 500
329+
326330
// Execute the primary callback
327331
typeof this._cb === 'function' && this._cb(err,res)
328332

lib/response.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ class RESPONSE {
462462
body: this._request.method === 'HEAD' ? '' : UTILS.encodeBody(body,this._serializer),
463463
isBase64Encoded: this._isBase64
464464
},
465-
this._request.interface === 'alb' ? { statusDescription: `${this._statusCode} ${UTILS.statusLookup(this._statusCode)}` } : {}
465+
this._request.interface === 'alb' ? { statusDescription: `${this._statusCode} ${UTILS.statusLookup(this._statusCode)}` } : {}
466466
)
467467

468468
// Trigger the callback function
@@ -475,7 +475,7 @@ class RESPONSE {
475475
error(code,e,detail) {
476476
detail = typeof code !== 'number' && e !== undefined ? e : detail
477477
e = typeof code !== 'number' ? code : e
478-
code = typeof code === 'number' ? code : 500
478+
code = typeof code === 'number' ? code : undefined
479479
this.app.catchErrors(e,this,code,detail)
480480
} // end error
481481

test/errorHandling.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ const api4 = require('../index')({ version: 'v1.0' })
1111
const api5 = require('../index')({ version: 'v1.0', logger: { access: 'never' }})
1212
const api_errors = require('../index')({ version: 'v1.0' })
1313

14+
class CustomError extends Error {
15+
constructor(message,code) {
16+
super(message)
17+
this.name = this.constructor.name
18+
this.code = code
19+
}
20+
}
21+
22+
1423
let event = {
1524
httpMethod: 'get',
1625
path: '/test',
@@ -86,6 +95,13 @@ const callError = (err,req,res,next) => {
8695

8796
api4.use(callError,errorMiddleware1)
8897

98+
api5.use((err,req,res,next) => {
99+
if (err instanceof CustomError) {
100+
res.status(401)
101+
}
102+
next()
103+
})
104+
89105
/******************************************************************************/
90106
/*** DEFINE TEST ROUTES ***/
91107
/******************************************************************************/
@@ -136,6 +152,10 @@ api5.get('/testErrorDetail', function(req,res) {
136152
res.error('This is a test error message','details')
137153
})
138154

155+
api5.get('/testErrorCustom', function(req,res) {
156+
throw new CustomError('This is a custom error',403)
157+
})
158+
139159
api_errors.use(function(err,req,res,next) {
140160
res.send({ errorType: err.name })
141161
});
@@ -287,6 +307,19 @@ describe('Error Handling Tests:', function() {
287307
expect(_log.detail).to.equal('details')
288308
}) // end it
289309

310+
it('Custom Error', async function() {
311+
let _log
312+
let _event = Object.assign({},event,{ path: '/testErrorCustom'})
313+
let logger = console.log
314+
console.log = log => { try { _log = JSON.parse(log) } catch(e) { _log = log } }
315+
let result = await new Promise(r => api5.run(_event,{},(e,res) => { r(res) }))
316+
console.log = logger
317+
// console.log(JSON.stringify(_log,null,2));
318+
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 401, body: '{"error":"This is a custom error"}', isBase64Encoded: false })
319+
expect(_log.level).to.equal('fatal')
320+
expect(_log.msg).to.equal('This is a custom error')
321+
}) // end it
322+
290323
})
291324

292325
}) // end ERROR HANDLING tests

0 commit comments

Comments
 (0)