updated error-pages example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 6f7075b · Browse files · 3 files·+41 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+41 added−54 removed
- other:+41 added−54 removed
Provenance
- Commit
6b47271- Tree
febd7da- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
6f7075b
3 files changed+41 added−54 removed
modified:examples/error-pages/views/404.jade+1 added-4 removed
1-- if (error.path)2- h2 Cannot find #{error.path}1+h2 Cannot find #{url}3-- else4- h2 Page Not Foundmodified:examples/error-pages/views/index.jade+4 added-1 removed
3 unmodified lines4 | visit 5 a(href="/500") 5006 li7 | visit8- a(href="/404") 4048+ a(href="/404") 4049+ li10+ | visit11+ a(href='/403') 403modified:examples/error-pages/app.js+36 added-49 removed
7 unmodified lines89101112131415161718191820212223242526272627282930312829303132343334353637383739404142434439454647484950414243444546474849505152535455565758596061626364656667686970715253545556575859606174757677787980626364656667878868697071727374757677787980812 unmodified lines7 unmodified lines
var express = require('../../lib/express');
var app = express.createServer();app.set('views', __dirname + '/views');app.set('view engine', 'jade');
// Serve default connect faviconapp.use(express.favicon());
// Logger is placed below favicon, so favicon.ico// requests will not be loggedapp.use(express.logger({ format: '":method :url" :status' }));app.use(express.logger('":method :url" :status'));
// "app.router" positions our routes // specifically above the middleware// assigned below
app.use(app.router);
// When no more middleware require execution, aka// our router is finished and did not respond, we// can assume that it is "not found". Instead of// letting Connect deal with this, we define our// custom middleware here to simply pass a NotFound// exception// Since this is the last non-error-handling// middleware use()d, we assume 404, as nothing else// responded.
app.use(function(req, res, next){ next(new NotFound(req.url)); // the status option, or res.statusCode = 404 // are equivalent, however with the option we // get the "status" local available as well res.render('404', { status: 404, url: req.url });});
app.set('views', __dirname + '/views');// error-handling middleware, take the same form// as regular middleware, however they require an// arity of 4, aka the signature (err, req, res, next).// when connect has an error, it will invoke ONLY error-handling// middleware.
// Provide our app with the notion of NotFound exceptions// If we were to next() here any remaining non-error-handling// middleware would then be executed, or if we next(err) to// continue passing the error, only error-handling middleware// would remain being executed, however here// we simply respond with an error page.
function NotFound(path){ this.name = 'NotFound'; if (path) { Error.call(this, 'Cannot find ' + path); this.path = path; } else { Error.call(this, 'Not Found'); } Error.captureStackTrace(this, arguments.callee);}
/** * Inherit from `Error.prototype`. */
NotFound.prototype.__proto__ = Error.prototype;
// We can call app.error() several times as shown below.// Here we check for an instanceof NotFound and show the// 404 page, or we pass on to the next error handler.
// These handlers could potentially be defined within// configure() blocks to provide introspection when// in the development environment.
app.error(function(err, req, res, next){ if (err instanceof NotFound) { res.render('404.jade', { status: 404, error: err }); } else { next(err); }app.use(function(err, req, res, next){ // we may use properties of the error object // here and next(err) appropriately, or if // we possibly recovered from the error, simply next(). res.render('500', { status: err.status || 500 , error: err });});
// Here we assume all errors as 500 for the simplicity of// this demo, however you can choose whatever you like
app.error(function(err, req, res){ res.render('500.jade', { status: 500, error: err });});
// Routes
app.get('/', function(req, res){ res.render('index.jade');});
app.get('/404', function(req, res){ throw new NotFound(req.url);app.get('/404', function(req, res, next){ next();});
app.get('/403', function(req, res, next){ var err = new Error('not allowed!'); err.status = 403; next(err);});
app.get('/500', function(req, res, next){ next(new Error('keyboard cat!'));});
2 unmodified lines