Skip to content
Myr

Added DSL level error() route support

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

authored by visionmedia · committed by visionmedia · parent a6dd697 · Browse files · 4 files+39 added−6 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+39 added−6 removed

  • tests:+12 added−0 removed
  • other:+27 added−6 removed

4 files+39 added−6 removed

modified:lib/express/spec/mocks.js+1 added
modified:lib/express/dsl.js+12 added-1 removed
130 unmodified lines
131 Express.params[key] = fn132}133134/**135 * Register a 404 "Not Found" route.135 * Register a "Not Found" route.136 *137 * @param {function} fn138 * @api public139 */
1 unmodified line
141exports.notFound = function(fn) {142 Express.notFound = fn143}144145/**146 * Register an "error" route.147 *148 * @param {function} fn149 * @api public150 */151152exports.error = function(fn) {153 Express.error = fn154}155156// --- Routing API157158exports.get = exports.view = route('get')159exports.post = exports.create = route('post')
2 unmodified lines
modified:lib/express/request.js+15 added-5 removed
292 unmodified lines
293 294 /**295 * Handle exceptions.296 *297 * When an error route is defined via the DSL it298 * will then be called regardless of any setting that299 * may be present. ("throw exceptions" will still work)300 *301 * error(function(){302 * this.halt(500, 'your app sucks!')303 * })304 *305 * When "html" is accepted, and "show exceptions" is enabled306 * the show-exceptions page will be shown. Otherwise307 * the request will halt with default 500 status body.308 *
6 unmodified lines
315 * @api public316 */317 318 error: function(err) {319 if (Express.error)320 Express.error.call(this)311 if (this.accepts('html') && set('show exceptions'))321 else if (this.accepts('html') && set('show exceptions'))322 this.halt(500, showExceptions.render(this, err))323 else324 this.halt(500)325 if (set('throw exceptions'))
4 unmodified lines
330 /**331 * Halt with 404 Not Found.332 *333 * When a notFound route is defined via the DSL it324 * will then be called regardless or any settings that334 * will then be called regardless of any settings that335 * may be present.336 *327 * notFound(function(){328 * this.halt(404, 'Sorry your page cannot be found')329 * })337 * notFound(function(){338 * this.halt(404, 'Sorry your page cannot be found')339 * })340 *341 * When "html" is accepted, and "helpful 404" is enabled342 * the not-found page will be shown, otherwise the 343 * request will halt with default 404 status body.
15 unmodified lines
modified:spec/spec.request.js+11 added
242 unmodified lines
243 end244 end245 246 describe '#error()'247 describe 'when an error route is defined'248 it 'should be called'249 disable('throw exceptions')250 error(function(){251 this.halt(500, 'FAIL!')252 })253 get('/', function(){ this.error(new Error('whoop')) })254 get('/').body.should.eql 'FAIL!'255 end256 end257 258 describe 'when accepting "html"'259 describe 'with "show exceptions" enabled'260 it 'should render the show-exceptions page'261 disable('throw exceptions')
103 unmodified lines