Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /**
  2. * Modified from the Connect project: https://github.com/senchalabs/connect/blob/master/lib/middleware/errorHandler.js
  3. *
  4. * Sample on:
  5. * http://mikevalstar.com/Blog/105/Coding_with_Nodejs_Part_2_Error_Handling_and_404_pages_with_Express
  6. *
  7. * Source on:
  8. * https://github.com/mikevalstar/mikevalstar_com/lib
  9. *
  10. * Flexible error handler, providing (_optional_) stack traces and logging
  11. * and error message responses for requests accepting text, html, or json.
  12. *
  13. * Options:
  14. *
  15. * - `showStack` respond with both the error message and stack trace. Defaults to `false`
  16. * - `showMessage`, respond with the exception message only. Defaults to `false`
  17. * - `dumpExceptions`, dump exceptions to stderr (without terminating the process). Defaults to `false`
  18. * - `logErrors`, will dump a log entry and stack trace into the gievn file. Defaults to `false`
  19. *
  20. * Text:
  21. * By default, and when _text/plain_ is accepted a simple stack trace
  22. * or error message will be returned.
  23. *
  24. * JSON:
  25. * When _application/json_ is accepted, connect will respond with
  26. * an object in the form of `{ "error": error }`.
  27. *
  28. * HTML:
  29. * When accepted connect will output a nice html stack trace.
  30. *
  31. * @param {Object} options
  32. * @return {Function}
  33. * @api public
  34. */
  35.  
  36. var fs = require('fs');
  37.  
  38. exports = module.exports = function errorHandler(options) {
  39. options = options || {};
  40. // defaults
  41. var showStack = options.showStack
  42. , showMessage = options.showMessage
  43. , dumpExceptions = options.dumpExceptions
  44. , logErrors = options.logErrors;
  45.  
  46. return function errorHandler(err, req, res, next) {
  47. res.statusCode = 500;
  48.  
  49. if (dumpExceptions) console.error(err.stack);
  50.  
  51. if (logErrors) {
  52. var now = new Date();
  53. fs.appendFile(logErrors, now.toJSON() + ' - Error Happened: \n' + err.stack + "\n", function(err, result) {
  54. });
  55. }
  56.  
  57. var accept = req.headers.accept || '';
  58. if (showStack) {
  59. // html
  60. if (~accept.indexOf('html')) {
  61. var stack = err.stack || '';
  62. stack = stack.
  63. replace(/&/g, '&').
  64. replace(/</g, '&lt;').
  65. replace(/"/g, '&quot;').
  66. replace(/'/g, '&#039;').
  67. replace(/\n/g, '<br />');
  68.  
  69. var error = err.toString();
  70. error = error.
  71. replace(/&/g, '&amp;').
  72. replace(/</g, '&lt;').
  73. replace(/"/g, '&quot;').
  74. replace(/'/g, '&#039;').
  75. replace(/\n/g, '<br />');
  76.  
  77. res.render('errorView.' + req.app.get('view engine'), { stack: stack, error: error });
  78. // json
  79. } else if (~accept.indexOf('json')) {
  80. var json = JSON.stringify({ error: err.toString() });
  81. res.setHeader('Content-Type', 'application/json');
  82. res.end(json);
  83. // plain text
  84. } else {
  85. res.writeHead(500, { 'Content-Type': 'text/plain' });
  86. res.end(err.stack);
  87. }
  88. } else {
  89. // public error page render
  90. // html
  91. if (~accept.indexOf('html')) {
  92. res.render('errorView', options.defs);
  93. // json
  94. } else if (~accept.indexOf('json')) {
  95. var json = JSON.stringify({ error: "There was a server error generating the content." });
  96. res.setHeader('Content-Type', 'application/json');
  97. res.end(json);
  98. // plain text
  99. } else {
  100. res.writeHead(500, { 'Content-Type': 'text/plain' });
  101. res.end("500 - Server Error");
  102. }
  103. }
  104. };
  105. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement