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
Provenance
- Commit
ed19885- Tree
773b4e6- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
297ae6f
1 file changed+9 added−58 removed
modified:docs/guide.md+9 added-58 removed
419 unmodified lines420 });421 422 ### Error Handling423 424-Express provides the _app.error()_ method which receives exceptions thrown within a route,425-or passed to _next(err)_. Below is an example which serves different pages based on our426-ad-hoc _NotFound_ exception:427-428- 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- });434 444-We can call _app.error()_ several times as shown below.445-Here we check for an instanceof _NotFound_ and show the446-404 page, or we pass on to the next error handler.447-448-Note that these handlers can be defined anywhere, as they449-will be placed below the route handlers on _listen()_. This 450-allows for definition within _configure()_ blocks so we can451-handle exceptions in different ways based on the environment.452-453- app.error(function(err, req, res, next){454- if (err instanceof NotFound) {455- res.render('404.jade');456- } else {457- next(err);458- }459- });460-461-Here we assume all errors as 500 for the simplicity of462-this 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.463-464- app.error(function(err, req, res){465- res.render('500.jade', {466- error: err467- });468- });469-470-Our apps could also utilize the Connect _errorHandler_ middleware471-to report on exceptions. For example if we wish to output exceptions 472-in "development" mode to _stderr_ we can use:473-474- app.use(express.errorHandler({ dumpExceptions: true }));475-476-Also during development we may want fancy html pages to show exceptions477-that are passed or thrown, so we can set _showStack_ to true:478-479- app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));480-481-The _errorHandler_ middleware also responds with _json_ if _Accept: application/json_482-is present, which is useful for developing apps that rely heavily on client-side JavaScript.483-435 ### Route Param Pre-conditions436 437 Route 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