refactor context: move the non-delegated properties up

This commit is contained in:
Jonathan Ong 2013-11-18 17:33:41 -08:00
parent 9407d7776c
commit b88babe5ee

View file

@ -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);
}
};