From b88babe5ee9515fd0a68a9b6203e96b66a4ecb6a Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Mon, 18 Nov 2013 17:33:41 -0800 Subject: [PATCH] refactor context: move the non-delegated properties up --- lib/context.js | 188 ++++++++++++++++++++++++------------------------- 1 file changed, 94 insertions(+), 94 deletions(-) diff --git a/lib/context.js b/lib/context.js index 5485ac2..654915d 100644 --- a/lib/context.js +++ b/lib/context.js @@ -15,6 +15,100 @@ var http = require('http'); module.exports = { + /** + * Inspect implementation. + * + * @return {Object} + * @api public + */ + + inspect: function(){ + return this.toJSON(); + }, + + /** + * Return JSON representation. + * + * @return {Object} + * @api public + */ + + toJSON: function(){ + return { + request: this.request.toJSON(), + response: this.response.toJSON() + } + }, + + /** + * Throw an error with `msg` and optional `status` + * defaulting to 500. Note that these are user-level + * errors, and the message may be exposed to the client. + * + * this.error(403) + * this.error('name required', 400) + * this.error('something exploded') + * + * @param {String|Number} msg + * @param {Number} status + * @api public + */ + + error: function(msg, status){ + // TODO: switch order... feels weird now that im used to express + if ('number' == typeof msg) { + var tmp = msg; + msg = http.STATUS_CODES[tmp]; + status = tmp; + } + + var err = new Error(msg); + err.status = status || 500; + err.expose = true; + throw err; + }, + + /** + * Default error handling. + * + * @param {Error} err + * @api private + */ + + onerror: function(err){ + // don't do anything if there is no error. + // this allows you to pass `this.onerror` + // to node-style callbacks. + if (!err) return; + + // nothing we can do here other + // than delegate to the app-level + // handler and log. + if (this.headerSent) { + err.headerSent = true; + this.app.emit('error', err, this); + return; + } + + // delegate + this.app.emit('error', err, this); + + // force text/plain + this.type = 'text'; + + // ENOENT support + if ('ENOENT' == err.code) err.status = 404; + + // default to 500 + err.status = err.status || 500; + + // respond + var code = http.STATUS_CODES[err.status]; + var msg = err.expose ? err.message : code; + this.status = err.status; + this.res.end(msg); + }, + /** * Delegate to Request#header. */ @@ -343,98 +437,4 @@ module.exports = { attachment: function() { return this.response.attachment.apply(this.response, arguments); }, - - /** - * Inspect implementation. - * - * @return {Object} - * @api public - */ - - inspect: function(){ - return this.toJSON(); - }, - - /** - * Return JSON representation. - * - * @return {Object} - * @api public - */ - - toJSON: function(){ - return { - request: this.request.toJSON(), - response: this.response.toJSON() - } - }, - - /** - * Throw an error with `msg` and optional `status` - * defaulting to 500. Note that these are user-level - * errors, and the message may be exposed to the client. - * - * this.error(403) - * this.error('name required', 400) - * this.error('something exploded') - * - * @param {String|Number} msg - * @param {Number} status - * @api public - */ - - error: function(msg, status){ - // TODO: switch order... feels weird now that im used to express - if ('number' == typeof msg) { - var tmp = msg; - msg = http.STATUS_CODES[tmp]; - status = tmp; - } - - var err = new Error(msg); - err.status = status || 500; - err.expose = true; - throw err; - }, - - /** - * Default error handling. - * - * @param {Error} err - * @api private - */ - - onerror: function(err){ - // don't do anything if there is no error. - // this allows you to pass `this.onerror` - // to node-style callbacks. - if (!err) return; - - // nothing we can do here other - // than delegate to the app-level - // handler and log. - if (this.headerSent) { - err.headerSent = true; - this.app.emit('error', err, this); - return; - } - - // delegate - this.app.emit('error', err, this); - - // force text/plain - this.type = 'text'; - - // ENOENT support - if ('ENOENT' == err.code) err.status = 404; - - // default to 500 - err.status = err.status || 500; - - // respond - var code = http.STATUS_CODES[err.status]; - var msg = err.expose ? err.message : code; - this.status = err.status; - this.res.end(msg); - } };