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
Provenance
- Commit
b958393- Tree
4d7547b- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
a6dd697
4 files changed+39 added−6 removed
modified:lib/express/spec/mocks.js+1 added
144 unmodified linesExpand all14514614714814915015115215324 unmodified lines144 unmodified linesExpand all Express.plugins = [] Express.settings = {} Express.params = {} delete Express.notFound delete Express.error configure('test')}
/**24 unmodified linesmodified:lib/express/dsl.js+12 added-1 removed
130 unmodified lines131 Express.params[key] = fn132 }133 134 /**135- * Register a 404 "Not Found" route.135+ * Register a "Not Found" route.136 *137 * @param {function} fn138 * @api public139 */1 unmodified line141 exports.notFound = function(fn) {142 Express.notFound = fn143 }144 145+/**146+ * Register an "error" route.147+ *148+ * @param {function} fn149+ * @api public150+ */151+152+exports.error = function(fn) {153+ Express.error = fn154+}155+156 // --- Routing API157 158 exports.get = exports.view = route('get')159 exports.post = exports.create = route('post')2 unmodified linesmodified:lib/express/request.js+15 added-5 removed
292 unmodified lines293 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 lines315 * @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 lines330 /**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 linesmodified:spec/spec.request.js+11 added
242 unmodified lines243 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