cozy-proxy
Version:
Cozy Proxy redirects requests properly to the right application of the Cozy platform depending on given path. It also handles authentication to the Cozy for users and devices.
41 lines (38 loc) • 1.12 kB
JavaScript
// Generated by CoffeeScript 1.10.0
var logger;
logger = require('printit')({
date: true,
prefix: 'app:error'
});
module.exports = function(err, req, res, next) {
var content, header, message, ref, statusCode, value;
if (err instanceof Error) {
logger.error(err.message);
logger.error(err.stack);
}
statusCode = err.status || 500;
message = err instanceof Error ? err.message : err.error;
message = message || 'Server error occurred';
if ((err.headers != null) && Object.keys(err.headers).length > 0 && !res.headersSent) {
ref = err.headers;
for (header in ref) {
value = ref[header];
res.set(header, value);
}
}
content = {
error: message
};
if (err.errors) {
content.errors = err.errors;
}
if (res.headersSent) {
return res.end(content);
} else if ((err.template != null) && (req != null ? req.accepts('html') : void 0) === 'html') {
return res.render(err.template.name, err.template.params, function(err, html) {
return res.status(statusCode).send(html);
});
} else {
return res.status(statusCode).send(content);
}
};