Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('It should be possible to pass a custom logger function', () => {
const expectedError = new createError.UnprocessableEntity()
const logger = jest.fn()
const handler = middy((event, context, cb) => {
throw expectedError
})
handler
.use(httpErrorHandler({ logger }))
// run the handler
handler({}, {}, (_, response) => {
expect(logger).toHaveBeenCalledWith(expectedError)
})
})
})
export function invalidRequestBody(): HttpErrors.HttpError {
return Object.assign(
new HttpErrors.UnprocessableEntity(INVALID_REQUEST_BODY_MESSAGE),
{
code: 'VALIDATION_FAILED',
},
);
}
before: (handler, next) => {
opts = opts || {}
const { headers } = handler.event
if (!headers) {
return next()
}
const contentType = headers['Content-Type'] || headers['content-type']
if (contentType) {
const { type } = contentTypeLib.parse(contentType)
if (type === 'application/json') {
try {
handler.event.body = JSON.parse(handler.event.body, opts.reviver)
} catch (err) {
throw new createError.UnprocessableEntity('Content type defined as JSON but an invalid JSON was provided')
}
}
}
next()
}
})
unprocessableEntity: function unprocessableEntity (message) {
return new createError.UnprocessableEntity(message)
},