Skip to content
Myr

removed old error handling docs

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 297ae6f · Browse files · 1 file+9 added−58 removed
This commit

State

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

Diff stats

1 file+9 added−58 removed

  • other:+9 added−58 removed

1 file+9 added−58 removed

modified:docs/guide.md+9 added-58 removed
419 unmodified lines
420 });421422### Error Handling423424Express provides the _app.error()_ method which receives exceptions thrown within a route,425or passed to _next(err)_. Below is an example which serves different pages based on our426ad-hoc _NotFound_ exception:427428 function NotFound(msg){429 this.name = 'NotFound';430 Error.call(this, msg);431 Error.captureStackTrace(this, arguments.callee);432 }433 434 NotFound.prototype.__proto__ = Error.prototype;435 436 app.get('/404', function(req, res){437 throw new NotFound;424 Error handling middleware are simply middleware with an arity of 4, aka425 the signature _(err, req, res, next)_. When you _next(err)_ an error,426 only these middleware are executed and have a chance to respond. For example:427 428 app.use(app.bodyParser());429 app.use(app.methodOverride());430 app.use(app.router);431 app.use(function(err, req, res, next){432 res.send('Internal Server Error', 500);433 });439 440 app.get('/500', function(req, res){441 throw new Error('keyboard cat!');442 });434444We can call _app.error()_ several times as shown below.445Here we check for an instanceof _NotFound_ and show the446404 page, or we pass on to the next error handler.447448Note that these handlers can be defined anywhere, as they449will be placed below the route handlers on _listen()_. This 450allows for definition within _configure()_ blocks so we can451handle exceptions in different ways based on the environment.452453 app.error(function(err, req, res, next){454 if (err instanceof NotFound) {455 res.render('404.jade');456 } else {457 next(err);458 }459 });460461Here we assume all errors as 500 for the simplicity of462this demo, however you can choose whatever you like. For example when node performs filesystem syscalls, you may receive an error object with the _error.code_ of _ENOENT_, meaning "no such file or directory", we can utilize this in our error handling and display a page specific to this if desired.463464 app.error(function(err, req, res){465 res.render('500.jade', {466 error: err467 });468 });469470Our apps could also utilize the Connect _errorHandler_ middleware471to report on exceptions. For example if we wish to output exceptions 472in "development" mode to _stderr_ we can use:473474 app.use(express.errorHandler({ dumpExceptions: true }));475476Also during development we may want fancy html pages to show exceptions477that are passed or thrown, so we can set _showStack_ to true:478479 app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));480481The _errorHandler_ middleware also responds with _json_ if _Accept: application/json_482is present, which is useful for developing apps that rely heavily on client-side JavaScript.483435### Route Param Pre-conditions436437Route param pre-conditions can drastically improve the readability of your application, through implicit loading of data, and validation of request urls. For example if you are constantly fetching common data for several routes, such as loading a user for _/user/:id_, we might typically do something like below:438
937 unmodified lines