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 de81ee7 · Browse files · 3 files·+214 added−0 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+214 added−0 removed
- other:+214 added−0 removed
Provenance
- Commit
9fbb9fa- Tree
96b269d- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
de81ee7
3 files changed+214 added−0 removed
modified:docs/guide.1+93 added
264 unmodified lines265 .fi266 .267 .IP "" 0268 .269+.P270+Alternatively we can \fIuse()\fR them which is useful when adding middleware within \fIconfigure()\fR blocks:271+.272+.IP "" 4273+.274+.nf275+276+app\.use(\'/\', connect\.logger({ format: \':method :uri\' }));277+.278+.fi279+.280+.IP "" 0281+.282+.SS "Error Handling"283+Express provides the \fIapp\.error()\fR method which receives exceptions thrown within a route, or passed to \fInext(err)\fR\. Below is an example which serves different pages based on our ad\-hoc \fINotFound\fR exception:284+.285+.IP "" 4286+.287+.nf288+289+function NotFound(msg){290+ this\.name = \'NotFound\';291+ Error\.call(this, msg);292+ Error\.captureStackTrace(this, arguments\.callee);293+}294+295+sys\.inherits(NotFound, Error);296+297+app\.get(\'/404\', function(req, res){298+ throw new NotFound;299+});300+301+app\.get(\'/500\', function(req, res){302+ throw new Error(\'keyboard cat!\');303+});304+.305+.fi306+.307+.IP "" 0308+.309+.P310+We can call \fIapp\.error()\fR several times as shown below\. Here we check for an instanceof \fINotFound\fR and show the 404 page, or we pass on to the next error handler\.311+.312+.IP "" 4313+.314+.nf315+316+app\.error(function(err, req, res, next){317+ if (err instanceof NotFound) {318+ res\.render(\'404\.jade\');319+ } else {320+ next(err);321+ }322+});323+.324+.fi325+.326+.IP "" 0327+.328+.P329+Here we assume all errors as 500 for the simplicity of this demo, however you can choose whatever you like330+.331+.IP "" 4332+.333+.nf334+335+app\.error(function(err, req, res){336+ res\.render(\'500\.jade\', {337+ locals: {338+ error: err339+ }340+ });341+});342+.343+.fi344+.345+.IP "" 0346+.347 .SS "req\.header(key[, defaultValue])"348 Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:349 .350 .IP "" 4337 unmodified lines688 .689 .fi690 .691 .IP "" 0692+.693+.SS "app\.error(function)"694+Adds an error handler \fIfunction\fR which will receive the exception as the first parameter:695+.696+.IP "" 4697+.698+.nf699 700+app\.error(function(err, req, res, next){701+ res\.send(err\.message, 500);702+});703+.704+.fi705+.706+.IP "" 0707+modified:docs/guide.html+64 added
171 unmodified lines172 <li><a href="#Settings">Settings</a></li>173 <li><a href="#Routing">Routing</a></li>174 <li><a href="#Passing-Route-Control">Passing Route Control</a></li>175 <li><a href="#Middleware">Middleware</a></li>176+<li><a href="#Error-Handling">Error Handling</a></li>177 <li><a href="#req-header-key-defaultValue-">req.header()</a></li>178 <li><a href="#req-accepts-type-">req.accepts()</a></li>179 <li><a href="#req-param-name-">req.param()</a></li>180 <li><a href="#req-flash-type-msg-">req.flash()</a></li>9 unmodified lines190 <li><a href="#app-enable-name-">app.enable()</a></li>191 <li><a href="#app-disable-name-">app.disable()</a></li>192 <li><a href="#app-configure-env-function-function-">app.configure()</a></li>193 <li><a href="#app-redirect-name-val-">app.redirect()</a></li>194+<li><a href="#app-error-function-">app.error()</a></li>195 </ul>196 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 197 <p id="tagline">198 High performance, high class web development for183 unmodified lines382 connect.bodyDecoder()383 );384 </code></pre>385 386+<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks:</p>387+388+<pre><code>app.use('/', connect.logger({ format: ':method :uri' }));389+</code></pre>390+391+<h3 id="Error-Handling">Error Handling</h3>392+393+<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,394+or passed to <em>next(err)</em>. Below is an example which serves different pages based on our395+ad-hoc <em>NotFound</em> exception:</p>396+397+<pre><code>function NotFound(msg){398+ this.name = 'NotFound';399+ Error.call(this, msg);400+ Error.captureStackTrace(this, arguments.callee);401+}402+403+sys.inherits(NotFound, Error);404+405+app.get('/404', function(req, res){406+ throw new NotFound;407+});408+409+app.get('/500', function(req, res){410+ throw new Error('keyboard cat!');411+});412+</code></pre>413+414+<p>We can call <em>app.error()</em> several times as shown below.415+Here we check for an instanceof <em>NotFound</em> and show the416+404 page, or we pass on to the next error handler.</p>417+418+<pre><code>app.error(function(err, req, res, next){419+ if (err instanceof NotFound) {420+ res.render('404.jade');421+ } else {422+ next(err);423+ }424+});425+</code></pre>426+427+<p>Here we assume all errors as 500 for the simplicity of428+this demo, however you can choose whatever you like</p>429+430+<pre><code>app.error(function(err, req, res){431+ res.render('500.jade', {432+ locals: {433+ error: err434+ } 435+ });436+});437+</code></pre>438+439 <h3 id="req-header-key-defaultValue-">req.header(key[, defaultValue])</h3>440 441 <p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>442 224 unmodified lines667 res.redirect('comments');668 });669 </code></pre>670 671+<h3 id="app-error-function-">app.error(function)</h3>672+673+<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter:</p>674+675+<pre><code>app.error(function(err, req, res, next){676+ res.send(err.message, 500);677+});678+</code></pre>679+680 </div>681 </div>682 </div>683 </body>1 unmodified linemodified:docs/guide.md+57 added
159 unmodified lines160 connect.logger(),161 connect.bodyDecoder()162 );163 164+Alternatively we can _use()_ them which is useful when adding middleware within _configure()_ blocks:165+166+ app.use('/', connect.logger({ format: ':method :uri' }));167+168+### Error Handling169+170+Express provides the _app.error()_ method which receives exceptions thrown within a route,171+or passed to _next(err)_. Below is an example which serves different pages based on our172+ad-hoc _NotFound_ exception:173+174+ function NotFound(msg){175+ this.name = 'NotFound';176+ Error.call(this, msg);177+ Error.captureStackTrace(this, arguments.callee);178+ }179+180+ sys.inherits(NotFound, Error);181+182+ app.get('/404', function(req, res){183+ throw new NotFound;184+ });185+186+ app.get('/500', function(req, res){187+ throw new Error('keyboard cat!');188+ });189+190+We can call _app.error()_ several times as shown below.191+Here we check for an instanceof _NotFound_ and show the192+404 page, or we pass on to the next error handler.193+194+ app.error(function(err, req, res, next){195+ if (err instanceof NotFound) {196+ res.render('404.jade');197+ } else {198+ next(err);199+ }200+ });201+202+Here we assume all errors as 500 for the simplicity of203+this demo, however you can choose whatever you like204+205+ app.error(function(err, req, res){206+ res.render('500.jade', {207+ locals: {208+ error: err209+ } 210+ });211+ });212+213 ### req.header(key[, defaultValue])214 215 Get the case-insensitive request header _key_, with optional _defaultValue_:216 201 unmodified lines418 419 app.get('/post/:id', function(req, res){420 res.redirect('comments');421 });422+423+### app.error(function)424+425+Adds an error handler _function_ which will receive the exception as the first parameter:426+427+ app.error(function(err, req, res, next){428+ res.send(err.message, 500);429+ });