Skip to content
Myr

Merge branch 'fix-error'

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 · parents 7dff9dd, 033ff06 · Browse files · 4 files+56 added−26 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+56 added−26 removed

  • tests:+11 added−11 removed
  • other:+45 added−15 removed

4 files+56 added−26 removed

modified:docs/guide.md+5 added
219 unmodified lines
220We can call _app.error()_ several times as shown below.221Here we check for an instanceof _NotFound_ and show the222404 page, or we pass on to the next error handler.223224Note that these handlers can be defined anywhere, as they225will be placed below the route handlers on _listen()_. This 226allows for definition within _configure()_ blocks so we can227handle exceptions in different ways based on the environment.228229 app.error(function(err, req, res, next){230 if (err instanceof NotFound) {231 res.render('404.jade');232 } else {
457 unmodified lines
modified:examples/pages/app.js+18 added-12 removed
modified:lib/express/server.js+22 added-3 removed
29 unmodified lines
30 this.settings = {};31 this.redirects = {};32 this.viewHelpers = {};33 this.dynamicViewHelpers = {};34 this.errorHandlers = [];35 connect.Server.call(this, middleware || []);3637 // Default "home" to / 38 this.set('home', '/');
33 unmodified lines
7273sys.inherits(Server, connect.Server);7475/**76 * Stack error handler callbacks, then listen on the given77 * `port` and `host`.78 *79 * @param {Number|String} port80 * @param {String} host81 * @return {Type}82 * @api public83 */8485Server.prototype.listen = function(){86 this.errorHandlers.forEach(function(fn){87 this.use(function(err, req, res, next){88 fn.apply(this, arguments);89 });90 }, this);91 connect.Server.prototype.listen.apply(this, arguments);92};9394/**95 * Proxy `connect.Server#use()` to apply settings to96 * mounted applications.97 *98 * @param {String|Function|Server} route
78 unmodified lines
177};178179/**180 * Assign a custom exception handler callback `fn`.181 * These handlers are always _last_ in the middleware stack.182 *183 * @param {Function} fn184 * @return {Server} for chaining185 * @api public186 */187188Server.prototype.error = function(fn){168 this.use(function(err, req, res, next){169 fn.apply(this, arguments);170 });189 this.errorHandlers.push(fn);190 return this;191};192193/**
86 unmodified lines
modified:test/express.test.js+11 added-11 removed
75 unmodified lines
7677 // Custom handler78 var app = express.createServer();79 80 app.error(function(err, req, res){81 res.send('Shit: ' + err.message, 500);82 });8384 app.get('/', function(req, res, next){85 next(new Error('broken'));86 });87 84 app.error(function(err, req, res){85 res.send('Shit: ' + err.message, 500);86 });87 88 assert.response(app,89 { url: '/' },90 { body: 'Shit: broken', status: 500 });91 92 // Multiple error()s93 var app = express.createServer();94 95 app.get('/', function(req, res, next){96 throw new Error('broken');97 });98 app.get('/foo', function(req, res, next){99 throw new Error('oh noes');100 });101 95 app.error(function(err, req, res, next){96 if (err.message === 'broken') {97 next(err);98 } else {
3 unmodified lines
102 103 app.error(function(err, req, res, next){104 res.send(err.message, 500);105 });106107 app.get('/', function(req, res, next){108 throw new Error('broken');109 });110 app.get('/foo', function(req, res, next){111 throw new Error('oh noes');112 });113 114 assert.response(app,115 { url: '/' },116 { body: 'broken', status: 500 });
204 unmodified lines