refactored error-pages example

This commit is contained in:
Tj Holowaychuk 2010-11-25 13:44:52 -08:00
parent 4f4987e9ea
commit 1ac18f7903

View File

@ -8,8 +8,7 @@ require.paths.unshift(__dirname + '/../../support');
var express = require('./../../lib/express'); var express = require('./../../lib/express');
var app = express.createServer(), var app = express.createServer();
sys = require('sys');
// Serve default connect favicon // Serve default connect favicon
app.use(express.favicon()); app.use(express.favicon());
@ -32,7 +31,7 @@ app.use(app.router);
// exception // exception
app.use(function(req, res, next){ app.use(function(req, res, next){
next(new NotFound(req.url)); next(new NotFound(req.url));
}); });
app.set('views', __dirname + '/views'); app.set('views', __dirname + '/views');
@ -40,17 +39,21 @@ app.set('views', __dirname + '/views');
// Provide our app with the notion of NotFound exceptions // Provide our app with the notion of NotFound exceptions
function NotFound(path){ function NotFound(path){
this.name = 'NotFound'; this.name = 'NotFound';
if (path) { if (path) {
Error.call(this, 'Cannot find ' + path); Error.call(this, 'Cannot find ' + path);
this.path = path; this.path = path;
} else { } else {
Error.call(this, 'Not Found'); Error.call(this, 'Not Found');
} }
Error.captureStackTrace(this, arguments.callee); Error.captureStackTrace(this, arguments.callee);
} }
sys.inherits(NotFound, Error); /**
* Inherit from `Error.prototype`.
*/
NotFound.prototype.__proto__ = Error.prototype;
// We can call app.error() several times as shown below. // We can call app.error() several times as shown below.
// Here we check for an instanceof NotFound and show the // Here we check for an instanceof NotFound and show the
@ -62,14 +65,9 @@ sys.inherits(NotFound, Error);
app.error(function(err, req, res, next){ app.error(function(err, req, res, next){
if (err instanceof NotFound) { if (err instanceof NotFound) {
res.render('404.jade', { res.render('404.jade', { status: 404, error: err });
status: 404,
locals: {
error: err
}
});
} else { } else {
next(err); next(err);
} }
}); });
@ -77,26 +75,21 @@ app.error(function(err, req, res, next){
// this demo, however you can choose whatever you like // this demo, however you can choose whatever you like
app.error(function(err, req, res){ app.error(function(err, req, res){
res.render('500.jade', { res.render('500.jade', { status: 500, error: err });
status: 500,
locals: {
error: err
}
});
}); });
// Routes // Routes
app.get('/', function(req, res){ app.get('/', function(req, res){
res.render('index.jade'); res.render('index.jade');
}); });
app.get('/404', function(req, res){ app.get('/404', function(req, res){
throw new NotFound; throw new NotFound(req.url);
}); });
app.get('/500', function(req, res, next){ app.get('/500', function(req, res, next){
next(new Error('keyboard cat!')); next(new Error('keyboard cat!'));
}); });
app.listen(3000); app.listen(3000);