Skip to content

Instantly share code, notes, and snippets.

@brandonb927
Forked from mikevalstar/ErrorHandler.js
Last active February 22, 2022 01:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brandonb927/8136255997221bf0051a to your computer and use it in GitHub Desktop.
Save brandonb927/8136255997221bf0051a to your computer and use it in GitHub Desktop.
Modified again to use Handlebars template instead of Jade
/**
* Modified from the Connect project: https://github.com/senchalabs/connect/blob/master/lib/middleware/errorHandler.js
*
* Flexible error handler, providing (_optional_) stack traces and logging
* and error message responses for requests accepting text, html, or json.
*
* Options:
*
* - `showStack` respond with both the error message and stack trace. Defaults to `false`
* - `showMessage`, respond with the exception message only. Defaults to `false`
* - `dumpExceptions`, dump exceptions to stderr (without terminating the process). Defaults to `false`
* - `logErrors`, will dump a log entry and stack trace into the gievn file. Defaults to `false`
*
* Text:
* By default, and when _text/plain_ is accepted a simple stack trace
* or error message will be returned.
*
* JSON:
* When _application/json_ is accepted, connect will respond with
* an object in the form of `{ 'error': error }`.
*
* HTML:
* When accepted connect will output a nice html stack trace.
*
* @param {Object} options
* @return {Function}
* @api public
*/
var fs = require('fs');
exports = module.exports = function errorHandler (options) {
options = options || {};
// defaults
var showStack = options.showStack;
var showMessage = options.showMessage;
var dumpExceptions = options.dumpExceptions;
var logErrors = options.logErrors;
var logErrorsStream = false;
if (options.logErrors) {
logErrorsStream = fs.createWriteStream(logErrors, { 'flags': 'a', encoding: 'utf-8', mode: 0666 });
}
return function errorHandler (err, req, res, next) {
res.statusCode = 500;
if (dumpExceptions) {
console.error(err.stack);
}
if (logErrors) {
var now = new Date();
logErrorsStream.write(now.toJSON() + ' - Error Happened: \n' + err.stack + '\n');
}
var accept = req.headers.accept || '';
if (showStack) {
if (~accept.indexOf('html')) {
// html
res.render('error', {
stack: err.stack || '',
error: err.toString()
});
}
else if (~accept.indexOf('json')) {
// json
var json = JSON.stringify({ error: err });
res.setHeader('Content-Type', 'application/json');
res.end(json);
}
else {
// plain text
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(err.stack);
}
} else {
// public error page render
if (~accept.indexOf('html')) {
// html
res.render('error');
}
else if (~accept.indexOf('json')) {
// json
var json = JSON.stringify({ error: 'There was a server error generating the content.' });
res.setHeader('Content-Type', 'application/json');
res.end(json);
}
else {
// plain text
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('500 - Server Error');
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment