context: .error() -> .throw()

.error() still works for compatibility, but it will be removed in the
future. closes #94
This commit is contained in:
Jonathan Ong 2013-11-18 17:38:12 -08:00
parent b88babe5ee
commit 7d9c6ba66c
2 changed files with 23 additions and 10 deletions

View file

@ -45,16 +45,16 @@ module.exports = {
* defaulting to 500. Note that these are user-level * defaulting to 500. Note that these are user-level
* errors, and the message may be exposed to the client. * errors, and the message may be exposed to the client.
* *
* this.error(403) * this.throw(403)
* this.error('name required', 400) * this.throw('name required', 400)
* this.error('something exploded') * this.throw('something exploded')
* *
* @param {String|Number} msg * @param {String|Number} msg
* @param {Number} status * @param {Number} status
* @api public * @api public
*/ */
error: function(msg, status){ throw: function(msg, status){
// TODO: switch order... feels weird now that im used to express // TODO: switch order... feels weird now that im used to express
if ('number' == typeof msg) { if ('number' == typeof msg) {
var tmp = msg; var tmp = msg;
@ -68,6 +68,19 @@ module.exports = {
throw err; throw err;
}, },
/**
* 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){
this.throw(msg, status);
},
/** /**
* Default error handling. * Default error handling.
* *

View file

@ -2,12 +2,12 @@
var context = require('../context'); var context = require('../context');
var assert = require('assert'); var assert = require('assert');
describe('ctx.error(msg)', function(){ describe('ctx.throw(msg)', function(){
it('should set .status to 500', function(done){ it('should set .status to 500', function(done){
var ctx = context(); var ctx = context();
try { try {
ctx.error('boom'); ctx.throw('boom');
} catch (err) { } catch (err) {
assert(500 == err.status); assert(500 == err.status);
done(); done();
@ -15,12 +15,12 @@ describe('ctx.error(msg)', function(){
}) })
}) })
describe('ctx.error(msg, status)', function(){ describe('ctx.throw(msg, status)', function(){
it('should throw an error', function(done){ it('should throw an error', function(done){
var ctx = context(); var ctx = context();
try { try {
ctx.error('name required', 400); ctx.throw('name required', 400);
} catch (err) { } catch (err) {
assert('name required' == err.message); assert('name required' == err.message);
assert(400 == err.status); assert(400 == err.status);
@ -29,12 +29,12 @@ describe('ctx.error(msg, status)', function(){
}) })
}) })
describe('ctx.error(status)', function(){ describe('ctx.throw(status)', function(){
it('should throw an error', function(done){ it('should throw an error', function(done){
var ctx = context(); var ctx = context();
try { try {
ctx.error(400); ctx.throw(400);
} catch (err) { } catch (err) {
assert('Bad Request' == err.message); assert('Bad Request' == err.message);
assert(400 == err.status); assert(400 == err.status);