From 9bde84da5e486204f35949d701d6651691a9dfdb Mon Sep 17 00:00:00 2001 From: Eduardo Veras Date: Wed, 29 Sep 2021 11:33:04 -0400 Subject: [PATCH] Raw error returned on onPreResponse event --- README.md | 10 ++++++++++ lib/response.js | 5 ++++- package.json | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d6df5c1..6cf5b79 100644 --- a/README.md +++ b/README.md @@ -548,6 +548,16 @@ server.on('onPostHandler', async payload => { server.on('onPreResponse', async (response, event) => { console.log('onPreResponse', event); console.log('onPreResponse', response); + + /* + response.error v1.0.15+ + In case the script fails, this field return the raw error object. + Good to create custom error messages on the response. + */ + if(response.error){ + console.error(response.error); + } + }); ``` diff --git a/lib/response.js b/lib/response.js index 6139b45..266cda1 100644 --- a/lib/response.js +++ b/lib/response.js @@ -13,6 +13,7 @@ exports = module.exports = class { "content-type" : 'application/json' }; this.body = { "statusCode": this.statusCode }; + this.error = null; const httpStatus = new HttpCodes(); @@ -37,6 +38,7 @@ exports = module.exports = class { this.body.error = httpStatus[this.statusCode.toString()]; this.body.message = payload.message; this.body.stack = payload.stack.toString().split('\n'); + this.error = payload; } if(typeof payload=='string'){ @@ -77,7 +79,8 @@ exports = module.exports = class { "body": JSON.stringify(Object.assign( this.body, payload.metas - )) + )), + "error": this.error }; diff --git a/package.json b/package.json index 093f942..edc4a88 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "serverless-aws-router", "description": "Minimalist serverless router system for Node.JS", - "version": "1.0.14", + "version": "1.0.15", "repository": "https://github.com/eduardo-veras/serverless-aws-router", "main": "lib/index.js", "engines": {