From 28c4acbb93e1c0000b4992d80603cafba537cb14 Mon Sep 17 00:00:00 2001 From: Jeremy Daly Date: Tue, 29 Oct 2019 13:47:29 -0400 Subject: [PATCH] fix #133 --- index.js | 2 +- test/errorHandling.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index a2d082d..9b832ce 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,7 @@ class API { this._callbackName = props && props.callback ? props.callback.trim() : 'callback' this._mimeTypes = props && props.mimeTypes && typeof props.mimeTypes === 'object' ? props.mimeTypes : {} this._serializer = props && props.serializer && typeof props.serializer === 'function' ? props.serializer : JSON.stringify - this._errorHeaderWhitelist = props && (props.errorHeaderWhitelist || []).map(header => header.toLowerCase()) + this._errorHeaderWhitelist = props && Array.isArray(props.errorHeaderWhitelist) ? props.errorHeaderWhitelist.map(header => header.toLowerCase()) : [] // Set sampling info this._sampleCounts = {} diff --git a/test/errorHandling.js b/test/errorHandling.js index 40b5515..6635073 100644 --- a/test/errorHandling.js +++ b/test/errorHandling.js @@ -10,6 +10,7 @@ const api3 = require('../index')({ version: 'v1.0' }) 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' }) +const api6 = require('../index')() // no props class CustomError extends Error { constructor(message,code) { @@ -172,6 +173,10 @@ api_errors.get('/responseError', (req,res) => { res.redirect(310,'http://www.google.com') }) +api6.get('/testError', function(req,res) { + res.error('This is a test error message') +}) + /******************************************************************************/ /*** BEGIN TESTS ***/ @@ -320,6 +325,17 @@ describe('Error Handling Tests:', function() { expect(_log.msg).to.equal('This is a custom error') }) // end it + + it('Error, no props', async function() { + let _log + let _event = Object.assign({},event,{ path: '/testError'}) + let logger = console.log + console.log = log => { try { _log = JSON.parse(log) } catch(e) { _log = log } } + let result = await new Promise(r => api6.run(_event,{},(e,res) => { r(res) })) + console.log = logger + expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 500, body: '{"error":"This is a test error message"}', isBase64Encoded: false }) + }) // end it + }) }) // end ERROR HANDLING tests