2018-03-28 19:06:39 +08:00
|
|
|
var errorHandlerFactory = require('../services/error_handler_factory');
|
2018-02-17 01:21:06 +08:00
|
|
|
|
2018-03-28 16:48:08 +08:00
|
|
|
module.exports = function errorMiddleware() {
|
|
|
|
return function error(err, req, res, next) {
|
2018-03-28 19:06:39 +08:00
|
|
|
const errorHandler = errorHandlerFactory(err);
|
2018-03-28 19:35:19 +08:00
|
|
|
let errorResponse = errorHandler.getResponse();
|
2018-02-17 01:21:06 +08:00
|
|
|
|
|
|
|
if (global.settings.environment === 'development') {
|
2018-03-28 19:35:19 +08:00
|
|
|
errorResponse.stack = err.stack;
|
2018-02-17 01:21:06 +08:00
|
|
|
}
|
|
|
|
|
2018-03-28 16:48:08 +08:00
|
|
|
if (global.settings.environment !== 'test') {
|
2018-02-17 01:21:06 +08:00
|
|
|
// TODO: email this Exception report
|
|
|
|
console.error("EXCEPTION REPORT: " + err.stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force inline content disposition
|
|
|
|
res.header("Content-Disposition", 'inline');
|
|
|
|
|
2018-03-28 16:48:08 +08:00
|
|
|
if (req && req.profiler) {
|
|
|
|
req.profiler.done('finish');
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
2018-02-17 01:21:06 +08:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:35:19 +08:00
|
|
|
setErrorHeader(errorResponse, errorHandler.http_status, res);
|
2018-02-17 01:21:06 +08:00
|
|
|
|
|
|
|
res.header('Content-Type', 'application/json; charset=utf-8');
|
2018-03-28 18:50:07 +08:00
|
|
|
res.status(getStatusError(errorHandler, req));
|
2018-02-17 01:21:06 +08:00
|
|
|
if (req.query && req.query.callback) {
|
2018-03-28 19:35:19 +08:00
|
|
|
res.jsonp(errorResponse);
|
2018-02-17 01:21:06 +08:00
|
|
|
} else {
|
2018-03-28 19:35:19 +08:00
|
|
|
res.json(errorResponse);
|
2018-02-17 01:21:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (req && req.profiler) {
|
|
|
|
res.req.profiler.sendStats();
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-03-28 18:50:07 +08:00
|
|
|
function getStatusError(errorHandler, req) {
|
2018-02-17 01:21:06 +08:00
|
|
|
|
2018-03-28 18:50:07 +08:00
|
|
|
var statusError = errorHandler.http_status;
|
2018-02-17 01:21:06 +08:00
|
|
|
|
|
|
|
// JSONP has to return 200 status error
|
|
|
|
if (req && req.query && req.query.callback) {
|
|
|
|
statusError = 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
return statusError;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setErrorHeader(err, statusCode, res) {
|
|
|
|
let errorsLog = Object.assign({}, err);
|
|
|
|
|
|
|
|
errorsLog.statusCode = statusCode || 200;
|
|
|
|
errorsLog.message = errorsLog.error[0];
|
|
|
|
delete errorsLog.error;
|
|
|
|
|
|
|
|
res.set('X-SQLAPI-Errors', stringifyForLogs(errorsLog));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove problematic nested characters
|
|
|
|
* from object for logs RegEx
|
|
|
|
*
|
|
|
|
* @param {Object} object
|
|
|
|
*/
|
|
|
|
function stringifyForLogs(object) {
|
|
|
|
Object.keys(object).map(key => {
|
2018-03-28 16:48:08 +08:00
|
|
|
if (typeof object[key] === 'string') {
|
2018-02-17 01:21:06 +08:00
|
|
|
object[key] = object[key].replace(/[^a-zA-Z0-9]/g, ' ');
|
|
|
|
} else if (typeof object[key] === 'object') {
|
|
|
|
stringifyForLogs(object[key]);
|
|
|
|
} else if (object[key] instanceof Array) {
|
|
|
|
for (let element of object[key]) {
|
|
|
|
stringifyForLogs(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return JSON.stringify(object);
|
|
|
|
}
|