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
Provenance
- Commit
498115d- Tree
05faa1e- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
7dff9dd- Parent 2
033ff06
4 files changed+56 added−26 removed
modified:docs/guide.md+5 added
219 unmodified lines220 We can call _app.error()_ several times as shown below.221 Here we check for an instanceof _NotFound_ and show the222 404 page, or we pass on to the next error handler.223 224+Note that these handlers can be defined anywhere, as they225+will be placed below the route handlers on _listen()_. This 226+allows for definition within _configure()_ blocks so we can227+handle exceptions in different ways based on the environment.228+229 app.error(function(err, req, res, next){230 if (err instanceof NotFound) {231 res.render('404.jade');232 } else {457 unmodified linesmodified:examples/pages/app.js+18 added-12 removed
18 unmodified lines1920212223242526272829303132333423242526272829303132333411 unmodified lines4647484950515253545556575859606162636418 unmodified lines}
sys.inherits(NotFound, Error);
app.get('/', function(req, res){ res.render('index.jade');});
app.get('/404', function(req, res){ throw new NotFound;});
app.get('/500', function(req, res){ throw new Error('keyboard cat!');});
// 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'); } else {11 unmodified lines } });});
// Routes
app.get('/', function(req, res){ res.render('index.jade');});
app.get('/404', function(req, res){ throw new NotFound;});
app.get('/500', function(req, res){ throw new Error('keyboard cat!');});
app.listen(3000);No newline at end of fileNo newline at end of filemodified:lib/express/server.js+22 added-3 removed
29 unmodified lines30 this.settings = {};31 this.redirects = {};32 this.viewHelpers = {};33 this.dynamicViewHelpers = {};34+ this.errorHandlers = [];35 connect.Server.call(this, middleware || []);36 37 // Default "home" to / 38 this.set('home', '/');33 unmodified lines72 73 sys.inherits(Server, connect.Server);74 75 /**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+ */84+85+Server.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+};93+94+/**95 * Proxy `connect.Server#use()` to apply settings to96 * mounted applications.97 *98 * @param {String|Function|Server} route78 unmodified lines177 };178 179 /**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 */187 188 Server.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 };192 193 /**86 unmodified linesmodified:test/express.test.js+11 added-11 removed
75 unmodified lines76 77 // Custom handler78 var app = express.createServer();79 80+ app.error(function(err, req, res){81+ res.send('Shit: ' + err.message, 500);82+ });83+84 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 lines102 103 app.error(function(err, req, res, next){104 res.send(err.message, 500);105 });106+107+ 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