Skip to content

Commit

Permalink
close #98
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Feb 6, 2019
1 parent 9337a91 commit c05055f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ class API {

let message

// Set the status code
response.status(code ? code : this._errorStatus)

let info = {
detail,
statusCode: response._statusCode,
Expand All @@ -266,13 +269,11 @@ class API {
}

if (e instanceof Error) {
response.status(code ? code : this._errorStatus)
message = e.message
this.log.fatal(message, info)
this.log.fatal(message,info)
} else {
response.status(code)
message = e
this.log.error(message, info)
this.log.error(message,info)
}

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

// Reset global error code
this._errorStatus = 500

// Execute the primary callback
typeof this._cb === 'function' && this._cb(err,res)

Expand Down
4 changes: 2 additions & 2 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ class RESPONSE {
body: this._request.method === 'HEAD' ? '' : UTILS.encodeBody(body,this._serializer),
isBase64Encoded: this._isBase64
},
this._request.interface === 'alb' ? { statusDescription: `${this._statusCode} ${UTILS.statusLookup(this._statusCode)}` } : {}
this._request.interface === 'alb' ? { statusDescription: `${this._statusCode} ${UTILS.statusLookup(this._statusCode)}` } : {}
)

// Trigger the callback function
Expand All @@ -475,7 +475,7 @@ class RESPONSE {
error(code,e,detail) {
detail = typeof code !== 'number' && e !== undefined ? e : detail
e = typeof code !== 'number' ? code : e
code = typeof code === 'number' ? code : 500
code = typeof code === 'number' ? code : undefined
this.app.catchErrors(e,this,code,detail)
} // end error

Expand Down
33 changes: 33 additions & 0 deletions test/errorHandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const api4 = require('../index')({ version: 'v1.0' })
const api5 = require('../index')({ version: 'v1.0', logger: { access: 'never' }})
const api_errors = require('../index')({ version: 'v1.0' })

class CustomError extends Error {
constructor(message,code) {
super(message)
this.name = this.constructor.name
this.code = code
}
}


let event = {
httpMethod: 'get',
path: '/test',
Expand Down Expand Up @@ -86,6 +95,13 @@ const callError = (err,req,res,next) => {

api4.use(callError,errorMiddleware1)

api5.use((err,req,res,next) => {
if (err instanceof CustomError) {
res.status(401)
}
next()
})

/******************************************************************************/
/*** DEFINE TEST ROUTES ***/
/******************************************************************************/
Expand Down Expand Up @@ -136,6 +152,10 @@ api5.get('/testErrorDetail', function(req,res) {
res.error('This is a test error message','details')
})

api5.get('/testErrorCustom', function(req,res) {
throw new CustomError('This is a custom error',403)
})

api_errors.use(function(err,req,res,next) {
res.send({ errorType: err.name })
});
Expand Down Expand Up @@ -287,6 +307,19 @@ describe('Error Handling Tests:', function() {
expect(_log.detail).to.equal('details')
}) // end it

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

})

}) // end ERROR HANDLING tests

0 comments on commit c05055f

Please sign in to comment.