diff options
Diffstat (limited to 'lib/middleware.js')
-rw-r--r-- | lib/middleware.js | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/lib/middleware.js b/lib/middleware.js index 567620f..5f78b20 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -20,26 +20,31 @@ See the License for the specific language governing permissions and limitations const ejs = require('ejs'); const utils = require('./utils'); -const logAccess = require('./logging').logAccess; +const logging = require('./logging'); +const logAccess = logging.logAccess; +const logError = logging.logError; +const logWarn = logging.logWarn; const config = process.mainModule.exports.config; const locations = config.locations; -const logger = config.logSource.getLogger("errhandler"); -/* Assign a request ID to each incoming request */ +/* Assign a request ID and start time to each incoming request */ exports.assignId = function(req, res, next) { - req.dcaeReqId = utils.generateId(); + /* Use request ID from header if available, otherwise generate one */ + req.dcaeReqId = req.get('X-ECOMP-RequestID') || utils.generateId(); + req.startTime = new Date(); next(); }; + /* Error handler -- send error with JSON body */ exports.handleErrors = function(err, req, res, next) { - let status = err.status || 500; - let msg = err.message || err.body || 'unknown error' + var status = err.status || 500; + var msg = err.message || err.body || 'unknown error' res.status(status).type('application/json').send({status: status, message: msg }); logAccess(req, status, msg); if (status >= 500) { - logger.error(req.dcaeReqId + " Error: " + JSON.stringify({message: msg, code: err.code, status: status})); + logError(err, req); } }; @@ -53,7 +58,7 @@ exports.checkType = function(type){ next(); } else { - let err = new Error ('Content-Type must be \'' + type +'\''); + var err = new Error ('Content-Type must be \'' + type +'\''); err.status = 415; next (err); } @@ -65,7 +70,7 @@ exports.checkProps = function(props) { return function (req, res, next) { const missing = props.filter(function(p){return !utils.hasProperty(req.body,p);}); if (missing.length > 0) { - let err = new Error ('Request missing required properties: ' + missing.join(',')); + var err = new Error ('Request missing required properties: ' + missing.join(',')); err.status = 400; next(err); } @@ -83,7 +88,7 @@ exports.checkLocation = function(req, res, next) { next(); } else { - let err = new Error ('"' + req.body.dcae_service_location + '" is not a supported location'); + var err = new Error ('"' + req.body.dcae_service_location + '" is not a supported location'); err.status = 400; next(err); } @@ -97,14 +102,14 @@ exports.checkLocation = function(req, res, next) { exports.expandTemplates = function(req, res, next) { /* Build the context for rendering the template */ - let context = req.body; // start with the body of POST /events request + var context = req.body; // start with the body of POST /events request context.locations = req.dcae_locations; // location information from the location "map" in config file context.dcae_shareables = req.dcae_shareables; // local shareable components /* Expand the templates */ try { if (req.dcae_templates) { // There won't be any templates for an undeploy - let blueprints = req.dcae_templates.map(function (template) { + var blueprints = req.dcae_templates.map(function (template) { //TODO possibly compute intensive- is there a better way? return { blueprint: ejs.render(template.template, context), |