2013-11-13 17:01:15 +00:00
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var debug = require('debug')('koa:context');
|
2014-01-13 14:36:10 +00:00
|
|
|
var delegate = require('delegates');
|
2013-08-17 07:15:57 +00:00
|
|
|
var http = require('http');
|
|
|
|
|
|
|
|
/**
|
2014-01-13 14:36:10 +00:00
|
|
|
* Context prototype.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-13 14:36:10 +00:00
|
|
|
var proto = module.exports = {
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-19 01:33:41 +00:00
|
|
|
/**
|
|
|
|
* Inspect implementation.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
inspect: function(){
|
2013-12-29 19:20:51 +00:00
|
|
|
return {
|
|
|
|
request: this.request,
|
|
|
|
response: this.response
|
|
|
|
}
|
2013-11-19 01:33:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return JSON representation.
|
|
|
|
*
|
2013-12-29 18:19:21 +00:00
|
|
|
* Here we explicitly invoke .toJSON() on each
|
|
|
|
* object, as iteration will otherwise fail due
|
|
|
|
* to the getters and cause utilities such as
|
|
|
|
* clone() to fail.
|
|
|
|
*
|
2013-11-19 01:33:41 +00:00
|
|
|
* @return {Object}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
toJSON: function(){
|
|
|
|
return {
|
2013-12-29 18:19:21 +00:00
|
|
|
request: this.request.toJSON(),
|
|
|
|
response: this.response.toJSON()
|
2013-11-19 01:33:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2013-11-19 01:38:12 +00:00
|
|
|
* this.throw(403)
|
|
|
|
* this.throw('name required', 400)
|
2013-12-20 23:34:16 +00:00
|
|
|
* this.throw(400, 'name required')
|
2013-11-19 01:38:12 +00:00
|
|
|
* this.throw('something exploded')
|
2014-01-04 08:23:39 +00:00
|
|
|
* this.throw(new Error('invalid'), 400);
|
|
|
|
* this.throw(400, new Error('invalid'));
|
2013-11-19 01:33:41 +00:00
|
|
|
*
|
2014-01-04 08:23:39 +00:00
|
|
|
* @param {String|Number|Error} err, msg or status
|
|
|
|
* @param {String|Number|Error} err, msg or status
|
2013-11-19 01:33:41 +00:00
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2013-11-19 01:38:12 +00:00
|
|
|
throw: function(msg, status){
|
2013-11-19 01:33:41 +00:00
|
|
|
if ('number' == typeof msg) {
|
|
|
|
var tmp = msg;
|
2013-12-20 23:34:16 +00:00
|
|
|
msg = status || http.STATUS_CODES[tmp];
|
2013-11-19 01:33:41 +00:00
|
|
|
status = tmp;
|
|
|
|
}
|
|
|
|
|
2014-01-04 08:23:39 +00:00
|
|
|
var err = msg instanceof Error ? msg : new Error(msg);
|
2013-11-19 01:33:41 +00:00
|
|
|
err.status = status || 500;
|
2014-01-21 02:44:07 +00:00
|
|
|
err.expose = err.status < 500;
|
2013-11-19 01:33:41 +00:00
|
|
|
throw err;
|
|
|
|
},
|
|
|
|
|
2013-11-19 01:38:12 +00:00
|
|
|
/**
|
|
|
|
* Alias for .throw() for backwards compatibility.
|
|
|
|
* Do not use - will be removed in the future.
|
|
|
|
*
|
|
|
|
* @param {String|Number} msg
|
|
|
|
* @param {Number} status
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
|
|
|
error: function(msg, status){
|
2013-12-19 10:48:32 +00:00
|
|
|
console.warn('ctx.error is deprecated, use ctx.throw');
|
2013-11-19 01:38:12 +00:00
|
|
|
this.throw(msg, status);
|
|
|
|
},
|
|
|
|
|
2013-11-19 01:33:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2013-12-18 01:37:35 +00:00
|
|
|
if (this.headerSent || !this.socket.writable) {
|
2013-11-19 01:33:41 +00:00
|
|
|
err.headerSent = true;
|
|
|
|
this.app.emit('error', err, this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// delegate
|
|
|
|
this.app.emit('error', err, this);
|
|
|
|
|
2014-01-24 22:29:57 +00:00
|
|
|
// unset all headers
|
|
|
|
this.res._headers = {};
|
|
|
|
|
2013-11-19 01:33:41 +00:00
|
|
|
// 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;
|
2014-01-24 22:29:57 +00:00
|
|
|
this.length = Buffer.byteLength(msg);
|
2013-11-19 01:33:41 +00:00
|
|
|
this.res.end(msg);
|
2014-01-13 14:36:10 +00:00
|
|
|
}
|
|
|
|
};
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2014-01-13 14:36:10 +00:00
|
|
|
/**
|
|
|
|
* Response delegation.
|
|
|
|
*/
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2014-01-13 14:36:10 +00:00
|
|
|
delegate(proto, 'response')
|
|
|
|
.method('attachment')
|
|
|
|
.method('redirect')
|
|
|
|
.method('append')
|
|
|
|
.method('remove')
|
|
|
|
.method('vary')
|
2014-01-13 14:36:49 +00:00
|
|
|
.method('set')
|
2014-01-13 14:36:10 +00:00
|
|
|
.access('status')
|
|
|
|
.access('body')
|
2014-02-14 17:16:39 +00:00
|
|
|
.access('length')
|
|
|
|
.access('type')
|
2014-02-14 17:33:10 +00:00
|
|
|
.access('charset')
|
2014-01-13 14:36:10 +00:00
|
|
|
.getter('headerSent')
|
|
|
|
.setter('lastModified')
|
2014-02-14 17:16:39 +00:00
|
|
|
.setter('etag');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2014-01-13 14:36:10 +00:00
|
|
|
/**
|
|
|
|
* Request delegation.
|
|
|
|
*/
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2014-01-13 14:36:10 +00:00
|
|
|
delegate(proto, 'request')
|
|
|
|
.method('acceptsLanguages')
|
|
|
|
.method('acceptsEncodings')
|
|
|
|
.method('acceptsCharsets')
|
|
|
|
.method('accepts')
|
|
|
|
.method('get')
|
|
|
|
.method('is')
|
|
|
|
.access('querystring')
|
|
|
|
.access('idempotent')
|
|
|
|
.access('socket')
|
|
|
|
.access('search')
|
|
|
|
.access('method')
|
2014-01-13 14:36:49 +00:00
|
|
|
.access('query')
|
2014-01-13 14:36:10 +00:00
|
|
|
.access('path')
|
|
|
|
.access('host')
|
|
|
|
.access('url')
|
|
|
|
.getter('subdomains')
|
|
|
|
.getter('protocol')
|
|
|
|
.getter('header')
|
2014-01-13 14:36:49 +00:00
|
|
|
.getter('secure')
|
2014-01-13 14:36:10 +00:00
|
|
|
.getter('stale')
|
|
|
|
.getter('fresh')
|
|
|
|
.getter('ips')
|
2014-01-13 14:36:49 +00:00
|
|
|
.getter('ip');
|