Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

wrote out error handler for the next tutorial

  • Loading branch information...
commit 73bc97b0e36a13e9799fb93642eede0179b90020 1 parent f4eefa9
Mike Valstar authored
23 app.js
@@ -14,12 +14,16 @@ app.configure(function(){
14 14 app.use(express.static(__dirname + '/htdocs'));
15 15 });
16 16
  17 +var error = require('./lib/ErrorHandler');
  18 +
17 19 app.configure('development', function(){
18   - app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  20 + //app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  21 + app.use(error({ showMessage: true, dumpExceptions: true, showStack: true, logErrors: __dirname + '/log/error_log' }));
19 22 });
20 23
21 24 app.configure('production', function(){
22   - app.use(express.errorHandler());
  25 + //app.use(express.errorHandler());
  26 + app.use(error());
23 27 });
24 28
25 29 // Internal Page Handlers
@@ -27,6 +31,21 @@ var static_pages = require('./lib/StaticPages');
27 31 var sp = new static_pages();
28 32 sp.initPages(app);
29 33
  34 +// 404 Page
  35 +app.use(function(req, res, next){
  36 + res.writeHead(404);
  37 + res.render('404.jade', {title: "404 - Page Not Found", showFullNav: false, status: 404, url: req.url });
  38 +});
  39 +
  40 +// Example error pages
  41 +app.get('/ErrorExample', function(req, res, next){
  42 + next(new Error('keyboard cat!')); // trigger an error
  43 +});
  44 +
  45 +app.get('/ErrorExample2', function(req, res){
  46 + res.render('404.jade'); // force an error.. we did not set the title
  47 +});
  48 +
30 49
31 50 app.listen(3000);
32 51 console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
92 lib/ErrorHandler.js
... ... @@ -0,0 +1,92 @@
  1 +/**
  2 + * Modified from the Connect project: https://github.com/senchalabs/connect/blob/master/lib/middleware/errorHandler.js
  3 + *
  4 + * Flexible error handler, providing (_optional_) stack traces and logging
  5 + * and error message responses for requests accepting text, html, or json.
  6 + *
  7 + * Options:
  8 + *
  9 + * - `showStack` respond with both the error message and stack trace. Defaults to `false`
  10 + * - `showMessage`, respond with the exception message only. Defaults to `false`
  11 + * - `dumpExceptions`, dump exceptions to stderr (without terminating the process). Defaults to `false`
  12 + * - `logErrors`, will dump a log entry and stack trace into the gievn file. Defaults to `false`
  13 + *
  14 + * Text:
  15 + * By default, and when _text/plain_ is accepted a simple stack trace
  16 + * or error message will be returned.
  17 + *
  18 + * JSON:
  19 + * When _application/json_ is accepted, connect will respond with
  20 + * an object in the form of `{ "error": error }`.
  21 + *
  22 + * HTML:
  23 + * When accepted connect will output a nice html stack trace.
  24 + *
  25 + * @param {Object} options
  26 + * @return {Function}
  27 + * @api public
  28 + */
  29 +
  30 +var fs = require('fs');
  31 +
  32 +exports = module.exports = function errorHandler(options){
  33 + options = options || {};
  34 + // defaults
  35 + var showStack = options.showStack
  36 + , showMessage = options.showMessage
  37 + , dumpExceptions = options.dumpExceptions
  38 + , logErrors = options.logErrors
  39 + , logErrorsStream = false;
  40 +
  41 + if(options.logErrors)
  42 + logErrorsStream = fs.createWriteStream(logErrors, {'flags': 'a', encoding: 'utf-8', mode: 0666});
  43 +
  44 + return function errorHandler(err, req, res, next){
  45 + res.statusCode = 500;
  46 +
  47 + if(dumpExceptions) console.error(err.stack);
  48 +
  49 + if(logErrors){
  50 + var now = new Date();
  51 + logErrorsStream.write(now.toJSON() + ' - Error Happened: \n' + err.stack + "\n");
  52 + }
  53 +
  54 + var accept = req.headers.accept || '';
  55 + if(showStack) {
  56 + // html
  57 + if (~accept.indexOf('html')) {
  58 + res.render('error.jade', {
  59 + layout: false
  60 + , stack: err.stack || ''
  61 + , error: err.toString()
  62 + });
  63 + // json
  64 + } else if (~accept.indexOf('json')) {
  65 + var json = JSON.stringify({ error: err });
  66 + res.setHeader('Content-Type', 'application/json');
  67 + res.end(json);
  68 + // plain text
  69 + } else {
  70 + res.writeHead(500, { 'Content-Type': 'text/plain' });
  71 + res.end(err.stack);
  72 + }
  73 + }else{
  74 + // public error page render
  75 + // html
  76 + if (~accept.indexOf('html')) {
  77 + res.render('error.jade', {
  78 + layout: false
  79 + });
  80 + // json
  81 + } else if (~accept.indexOf('json')) {
  82 + var json = JSON.stringify({ error: "There was a server error generating the content." });
  83 + res.setHeader('Content-Type', 'application/json');
  84 + res.end(json);
  85 + // plain text
  86 + } else {
  87 + res.writeHead(500, { 'Content-Type': 'text/plain' });
  88 + res.end("500 - Server Error");
  89 + }
  90 + }
  91 + };
  92 +};
85 log/error_log
... ... @@ -0,0 +1,85 @@
  1 +2011-11-17T19:08:47.450Z - Error Happened:
  2 +Error: keyboard cat!
  3 + at /Users/mikevalstar/Dropbox/Public/mv_app/app.js:44:8
  4 + at callbacks (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:272:11)
  5 + at param (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:246:11)
  6 + at pass (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:253:5)
  7 + at Router._dispatch (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:280:4)
  8 + at Object.handle (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:45:10)
  9 + at next (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/http.js:201:15)
  10 + at Object.methodOverride [as handle] (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
  11 + at next (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/http.js:201:15)
  12 + at Object.bodyParser [as handle] (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:59:61)2011-11-17T19:08:58.254Z - Error Happened:
  13 +Error: keyboard cat!
  14 + at /Users/mikevalstar/Dropbox/Public/mv_app/app.js:44:8
  15 + at callbacks (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:272:11)
  16 + at param (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:246:11)
  17 + at pass (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:253:5)
  18 + at Router._dispatch (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:280:4)
  19 + at Object.handle (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:45:10)
  20 + at next (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/http.js:201:15)
  21 + at Object.methodOverride [as handle] (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
  22 + at next (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/http.js:201:15)
  23 + at Object.bodyParser [as handle] (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:59:61)2011-11-17T19:09:09.524Z - Error Happened:
  24 +Error: keyboard cat!
  25 + at /Users/mikevalstar/Dropbox/Public/mv_app/app.js:44:8
  26 + at callbacks (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:272:11)
  27 + at param (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:246:11)
  28 + at pass (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:253:5)
  29 + at Router._dispatch (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:280:4)
  30 + at Object.handle (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:45:10)
  31 + at next (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/http.js:201:15)
  32 + at Object.methodOverride [as handle] (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
  33 + at next (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/http.js:201:15)
  34 + at Object.bodyParser [as handle] (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:59:61)2011-12-09T18:33:18.956Z - Error Happened:
  35 +ReferenceError: /Users/mikevalstar/Dropbox/Public/mv_app/views/layout.jade:4
  36 + 2| html(lang="en")
  37 + 3| head
  38 + > 4| title #{title} - Mike Valstar - Programming & Web Development
  39 + 5| link(rel='stylesheet', href='/css/css.css')
  40 + 6|
  41 + 7| script(src='http://code.jquery.com/jquery-1.7.min.js', type='text/javascript')
  42 +
  43 +title is not defined
  44 + at eval at <anonymous> (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/jade/lib/jade.js:165:3)
  45 + at Object.<anonymous> (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/jade/lib/jade.js:166:12)
  46 + at ServerResponse._render (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/view.js:422:21)
  47 + at ServerResponse.render (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/view.js:315:17)
  48 + at ServerResponse._render (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/view.js:429:10)
  49 + at ServerResponse.render (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/view.js:315:17)
  50 + at /Users/mikevalstar/Dropbox/Public/mv_app/app.js:36:7
  51 + at callbacks (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:272:11)
  52 + at param (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:246:11)
  53 + at pass (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:253:5)
  54 +2011-12-09T18:34:11.142Z - Error Happened:
  55 +Error: keyboard cat!
  56 + at /Users/mikevalstar/Dropbox/Public/mv_app/app.js:44:8
  57 + at callbacks (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:272:11)
  58 + at param (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:246:11)
  59 + at pass (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:253:5)
  60 + at Router._dispatch (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:280:4)
  61 + at Object.handle (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:45:10)
  62 + at next (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/http.js:203:15)
  63 + at Object.methodOverride [as handle] (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
  64 + at next (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/http.js:203:15)
  65 + at Object.bodyParser [as handle] (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:85:61)
  66 +2011-12-09T18:36:52.500Z - Error Happened:
  67 +ReferenceError: /Users/mikevalstar/Dropbox/Public/mv_app/views/layout.jade:4
  68 + 2| html(lang="en")
  69 + 3| head
  70 + > 4| title #{title} - Mike Valstar - Programming &amp; Web Development
  71 + 5| link(rel='stylesheet', href='/css/css.css')
  72 + 6|
  73 + 7| script(src='http://code.jquery.com/jquery-1.7.min.js', type='text/javascript')
  74 +
  75 +title is not defined
  76 + at eval at <anonymous> (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/jade/lib/jade.js:165:3)
  77 + at Object.<anonymous> (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/jade/lib/jade.js:166:12)
  78 + at ServerResponse._render (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/view.js:422:21)
  79 + at ServerResponse.render (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/view.js:315:17)
  80 + at ServerResponse._render (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/view.js:429:10)
  81 + at ServerResponse.render (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/view.js:315:17)
  82 + at /Users/mikevalstar/Dropbox/Public/mv_app/app.js:36:7
  83 + at callbacks (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:272:11)
  84 + at param (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:246:11)
  85 + at pass (/Users/mikevalstar/Dropbox/Public/mv_app/node_modules/express/lib/router/index.js:253:5)
3  views/404.jade
... ... @@ -0,0 +1,3 @@
  1 +h2 404
  2 +
  3 +h3 Page Not Found
56 views/error.jade
... ... @@ -0,0 +1,56 @@
  1 +!!! 5
  2 +html(lang="en")
  3 +head
  4 + title Error - Mike Valstar - Programming &amp; Web Development
  5 + link(rel='stylesheet', href='/css/css.css')
  6 +
  7 + script(src='http://code.jquery.com/jquery-1.7.min.js', type='text/javascript')
  8 + script(src='/js/jquery.tweet.js', type='text/javascript')
  9 + script(src='/js/jquery.ba-hashchange.min.js', type='text/javascript')
  10 + script(src='/js/script.min.js', type='text/javascript')
  11 + script(src='/js/mikevalstar.js', type='text/javascript')
  12 +
  13 + //if lt IE 9
  14 + script(src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js")
  15 +
  16 + script(type="text/javascript")
  17 + var _gaq = _gaq || [];
  18 + _gaq.push(['_setAccount', 'UA-1984894-13']);
  19 +
  20 + (function() {
  21 + var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  22 + ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  23 + var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  24 + })();
  25 +
  26 +body#mikevalstar
  27 + #O
  28 +
  29 + #H
  30 + h1: a(href="http://mikevalstar.com/") Mike Valstar
  31 + #social
  32 + //- a(href="#"): img(src="/img/rss.png", alt="RSS")
  33 + a(href="https://github.com/mikevalstar"): img(src="/img/github.png", alt="Github")
  34 + a(href="https://plus.google.com/107235391839316017795"): img(src="/img/google-plus.png", alt="Google+")
  35 + a(href="http://twitter.com/#!/mikevalstar"): img(src="/img/twitter.png", alt="Twitter")
  36 +
  37 + h2 Web Development, Programming, Random Thoughts
  38 +
  39 + #Nav
  40 + ul
  41 + li#NBlog: a(href="/Blog") Blog
  42 + li#NProjects: a(href="/Projects") Projects
  43 + li#NAbout: a(href="/About") About
  44 + li#NTwitter: div Twitter Feed
  45 + div.clear
  46 +
  47 + #C
  48 + #CC
  49 + h2 500 Error
  50 +
  51 + - if(stack)
  52 + pre: code.stack!=stack
  53 + - else
  54 + There was an error rendering this page. Sorry for the inconvenience.
  55 +
  56 + #F Mike Valstar &copy; 2011

0 comments on commit 73bc97b

Please sign in to comment.
Something went wrong with that request. Please try again.