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
Provenance
- Commit
e9fdfc3- Tree
755ecf4- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
132730a
3 files changed+76 added−10 removed
modified:lib/express/pages/show-exceptions.js+52 added-3 removed
4 unmodified lines5678991011121311 unmodified lines252627282929303132333 unmodified lines373839404142434445464748495051525354555657585960616263646566676869707172737429 unmodified lines104105106107781081091101111121131141151161171181191201211221231241251261274 unmodified lines
/** * Return list items for exception _e_'s stack. * * @param {object} e * @param {Error} e * @return {string} * @api private */
11 unmodified lines
/** * Return table rows for _hash_. * * @param {hash} hash * @param {object} hash * @return {string} * @api private */
3 unmodified lines return '<tr><td>' + key + ':</td><td>' + JSON.encode(val) + '</td></tr>' }).join('\n')}
/** * Return plain-text list items for exception _e_'s stack. * * @param {Error} e * @return {string} * @api private */
function stackText(e) { if (e.stack) return e.stack.split('\n').slice(1).map(function(val, i){ return '\n ' + val.strip }).join('')}
/** * Return an ascii table for _hash_. * * @param {object} hash * @return {string} * @api private */
function hashText(hash) { if (!hash || !hash.values.length) return '\n Empty' return hash.map(function(val, key){ return '\n ' + key + ': ' + JSON.encode(val) }).join('')}
exports.render = function(request, e) { request.contentType('html') return '<html> \n\ <head> \n\29 unmodified lines </table> \n\ </div> \n\ </body> \n\ </html>'}No newline at end of file}
exports.renderText = function(request, e) { request.contentType('text') return '\ \n Express \ \n\n 500 ' + e + ' \ \n' + stackText(e) + ' \ \n\n Request\n ' + hashText(request.headers) + '\ \n\n Response\n ' + hashText(request.response.headers) + '\ \n\n Params\n ' + hashText(request.params.path) + '\ \n\n GET\n ' + hashText(request.params.get) + '\ \n\n POST\n ' + hashText(request.params.post) + '\n'}
exports.renderJSON = function(request, e) { request.contentType('json') delete e.stack return JSON.encode({ error: e })}modified:lib/express/request.js+12 added-5 removed
339 unmodified lines340 * 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 lines362 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 err32 unmodified linesmodified:spec/spec.request.js+12 added-2 removed
260 unmodified lines261 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 lines283 })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 lines329 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 end70 unmodified lines