diff options
Diffstat (limited to 'lib/middleware.js')
-rw-r--r-- | lib/middleware.js | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/lib/middleware.js b/lib/middleware.js index 183cf77..ee39863 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -1,16 +1,16 @@ /* -Copyright(c) 2017 AT&T Intellectual Property. All rights reserved. +Copyright(c) 2017-2018 AT&T Intellectual Property. All rights reserved. -Licensed under the Apache License, Version 2.0 (the "License"); +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -CONDITIONS OF ANY KIND, either express or implied. +CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ @@ -32,12 +32,19 @@ exports.assignId = function(req, res, next) { /* Error handler -- send error with JSON body */ exports.handleErrors = function(err, req, res, next) { - var status = err.status || 500; - var msg = err.message || err.body || 'unknown error' - res.status(status).type('application/json').send({status: status, message: msg }); - log.audit(req, status, msg); + const response = { + status : err.status || 500, + message : err.message || err.body || 'unknown error' + }; + if (err.stack) { + response.stack = err.stack.split("\n"); + } + + res.status(response.status).type('application/json').send(response); + log.audit(req, response.status, JSON.stringify(response)); - if (status >= 500) { + if (response.status >= 500) { + err.message = response.message + (err.stack && " " + response.stack.join(', ')); log.error(err, req); } }; @@ -55,7 +62,7 @@ exports.checkType = function(type){ var err = new Error ('Content-Type must be \'' + type +'\''); err.status = 415; next (err); - } + } }; }; @@ -70,7 +77,7 @@ exports.checkProps = function(props) { } else { next(); - } + } }; }; |