Skip to content
Myr

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

3 files+214 added−0 removed

modified:docs/guide.1+93 added
264 unmodified lines
265.fi266.267.IP "" 0268.269.P270Alternatively we can \fIuse()\fR them which is useful when adding middleware within \fIconfigure()\fR blocks:271.272.IP "" 4273.274.nf275276app\.use(\'/\', connect\.logger({ format: \':method :uri\' }));277.278.fi279.280.IP "" 0281.282.SS "Error Handling"283Express 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.nf288289function NotFound(msg){290 this\.name = \'NotFound\';291 Error\.call(this, msg);292 Error\.captureStackTrace(this, arguments\.callee);293}294295sys\.inherits(NotFound, Error);296297app\.get(\'/404\', function(req, res){298 throw new NotFound;299});300301app\.get(\'/500\', function(req, res){302 throw new Error(\'keyboard cat!\');303});304.305.fi306.307.IP "" 0308.309.P310We 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.nf315316app\.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.P329Here we assume all errors as 500 for the simplicity of this demo, however you can choose whatever you like330.331.IP "" 4332.333.nf334335app\.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])"348Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:349.350.IP "" 4
337 unmodified lines
688.689.fi690.691.IP "" 0692.693.SS "app\.error(function)"694Adds an error handler \fIfunction\fR which will receive the exception as the first parameter:695.696.IP "" 4697.698.nf699700app\.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 lines
172<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 lines
190<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 for
183 unmodified lines
382 connect.bodyDecoder()383);384</code></pre>385386<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks:</p>387388<pre><code>app.use('/', connect.logger({ format: ':method :uri' }));389</code></pre>390391<h3 id="Error-Handling">Error Handling</h3>392393<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,394or passed to <em>next(err)</em>. Below is an example which serves different pages based on our395ad-hoc <em>NotFound</em> exception:</p>396397<pre><code>function NotFound(msg){398 this.name = 'NotFound';399 Error.call(this, msg);400 Error.captureStackTrace(this, arguments.callee);401}402403sys.inherits(NotFound, Error);404405app.get('/404', function(req, res){406 throw new NotFound;407});408409app.get('/500', function(req, res){410 throw new Error('keyboard cat!');411});412</code></pre>413414<p>We can call <em>app.error()</em> several times as shown below.415Here we check for an instanceof <em>NotFound</em> and show the416404 page, or we pass on to the next error handler.</p>417418<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>426427<p>Here we assume all errors as 500 for the simplicity of428this demo, however you can choose whatever you like</p>429430<pre><code>app.error(function(err, req, res){431 res.render('500.jade', {432 locals: {433 error: err434 } 435 });436});437</code></pre>438439<h3 id="req-header-key-defaultValue-">req.header(key[, defaultValue])</h3>440441<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>442
224 unmodified lines
667 res.redirect('comments');668});669</code></pre>670671<h3 id="app-error-function-">app.error(function)</h3>672673<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter:</p>674675<pre><code>app.error(function(err, req, res, next){676 res.send(err.message, 500);677});678</code></pre>679680</div>681 </div>682 </div>683 </body>
1 unmodified line
modified:docs/guide.md+57 added
159 unmodified lines
160 connect.logger(),161 connect.bodyDecoder()162 );163164Alternatively we can _use()_ them which is useful when adding middleware within _configure()_ blocks:165166 app.use('/', connect.logger({ format: ':method :uri' }));167168### Error Handling169170Express provides the _app.error()_ method which receives exceptions thrown within a route,171or passed to _next(err)_. Below is an example which serves different pages based on our172ad-hoc _NotFound_ exception:173174 function NotFound(msg){175 this.name = 'NotFound';176 Error.call(this, msg);177 Error.captureStackTrace(this, arguments.callee);178 }179180 sys.inherits(NotFound, Error);181182 app.get('/404', function(req, res){183 throw new NotFound;184 });185186 app.get('/500', function(req, res){187 throw new Error('keyboard cat!');188 });189190We can call _app.error()_ several times as shown below.191Here we check for an instanceof _NotFound_ and show the192404 page, or we pass on to the next error handler.193194 app.error(function(err, req, res, next){195 if (err instanceof NotFound) {196 res.render('404.jade');197 } else {198 next(err);199 }200 });201202Here we assume all errors as 500 for the simplicity of203this demo, however you can choose whatever you like204205 app.error(function(err, req, res){206 res.render('500.jade', {207 locals: {208 error: err209 } 210 });211 });212213### req.header(key[, defaultValue])214215Get the case-insensitive request header _key_, with optional _defaultValue_:216
201 unmodified lines
418419 app.get('/post/:id', function(req, res){420 res.redirect('comments');421 });422423### app.error(function)424425Adds an error handler _function_ which will receive the exception as the first parameter:426427 app.error(function(err, req, res, next){428 res.send(err.message, 500);429 });