Skip to content
Myr

Added error reponse support for text/plain, application/json. Closes #297

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

authored by visionmedia · committed by visionmedia · parent 132730a · Browse files · 3 files+76 added−10 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+76 added−10 removed

  • tests:+12 added−2 removed
  • other:+64 added−8 removed

3 files+76 added−10 removed

modified:lib/express/pages/show-exceptions.js+52 added-3 removed
modified:lib/express/request.js+12 added-5 removed
339 unmodified lines
340 * error(function(err){341 * this.halt(500, 'your app sucks!')342 * })343 *344 * When "html" is accepted, and "show exceptions" is enabled345 * the show-exceptions page will be shown. Otherwise346 * the request will halt with default 500 status body.344 * When "show exceptions" is enabled the show-exceptions page will be shown for "text/html",345 * a plain-text representation of the error for "text/plain" and JSON for "application/json".346 * Otherwise the request will halt with default 500 status body.347 *348 * When "throw exceptions" is enabled the error will be349 * re-thrown, terminating the process (unless otherwise caught).350 *
11 unmodified lines
362 if (callback)363 return callback(err)364 if (Express.error)365 Express.error.call(this, err)366 else if (this.accepts('html') && set('show exceptions'))367 this.halt(500, showExceptions.render(this, err))366 else if (set('show exceptions'))367 if (this.accepts('html'))368 this.halt(500, showExceptions.render(this, err))369 else if (this.accepts('json'))370 this.halt(500, showExceptions.renderJSON(this, err))371 else if (this.accepts('text'))372 this.halt(500, showExceptions.renderText(this, err))373 else374 this.halt(500)375 else376 this.halt(500)377 if (set('throw exceptions'))378 throw err
32 unmodified lines
modified:spec/spec.request.js+12 added-2 removed
260 unmodified lines
261 end262 263 describe 'when not accepting common mime types'264 it 'should render the default 500 status body'265 var headers = { headers: { accept: 'text/plain' }}265 var headers = { headers: { accept: 'text/xml' }}266 disable('throw exceptions')267 enable('show exceptions')268 get('/', function(){269 this.error(new Error('fail!'))
13 unmodified lines
283 })284 get('/').body.should.include '<em>500</em> Error: fail!'285 get('/').status.should.eql 500286 end287 288 it 'should render the show-exceptions page with errors thrown within the route'289 disable('throw exceptions')290 enable('show exceptions')291 get('/', function(){292 throw new Error('fail!')293 })294 get('/').body.should.include '<em>500</em> Error: fail!'295 get('/').status.should.eql 500296 end297 end298 299 describe 'with "throw exceptions" enabled'300 it 'should re-throw the exception'
28 unmodified lines
329 enable('show exceptions')330 get('/', function(){331 this.error(new Error('fail!'))332 })323 get('/', { headers: { accept: 'application/json' }}).body.should.include '{ "error": { "message": "fail!" }}'333 get('/', { headers: { accept: 'application/json' }}).body.should.include '{"error":{"message":"fail!"'334 get('/').status.should.eql 500 335 end336 end337 end
70 unmodified lines