2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const context = require('../context');
|
|
|
|
const assert = require('assert');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2013-11-19 01:38:12 +00:00
|
|
|
describe('ctx.throw(msg)', function(){
|
2013-11-13 17:01:15 +00:00
|
|
|
it('should set .status to 500', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
try {
|
2013-11-19 01:38:12 +00:00
|
|
|
ctx.throw('boom');
|
2013-11-13 17:01:15 +00:00
|
|
|
} catch (err) {
|
|
|
|
assert(500 == err.status);
|
2014-08-06 13:31:55 +00:00
|
|
|
assert(!err.expose);
|
2013-11-13 17:01:15 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-01-04 08:23:39 +00:00
|
|
|
describe('ctx.throw(err)', function(){
|
|
|
|
it('should set .status to 500', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
|
|
|
const err = new Error('test');
|
2014-01-04 08:23:39 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.throw(err);
|
|
|
|
} catch (err) {
|
|
|
|
assert(500 == err.status);
|
|
|
|
assert('test' == err.message);
|
2014-08-06 13:31:55 +00:00
|
|
|
assert(!err.expose);
|
2014-01-04 08:23:39 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('ctx.throw(err, status)', function(){
|
|
|
|
it('should throw the error and set .status', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
|
|
|
const error = new Error('test');
|
2014-01-04 08:23:39 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.throw(error, 422);
|
|
|
|
} catch (err) {
|
|
|
|
assert(422 == err.status);
|
|
|
|
assert('test' == err.message);
|
2014-08-06 13:31:55 +00:00
|
|
|
assert(true === err.expose);
|
2014-01-04 08:23:39 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('ctx.throw(status, err)', function(){
|
|
|
|
it('should throw the error and set .status', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
|
|
|
const error = new Error('test');
|
2014-01-04 08:23:39 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.throw(422, error);
|
|
|
|
} catch (err) {
|
|
|
|
assert(422 == err.status);
|
|
|
|
assert('test' == err.message);
|
2014-08-06 13:31:55 +00:00
|
|
|
assert(true === err.expose);
|
2014-01-04 08:23:39 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2013-11-19 01:38:12 +00:00
|
|
|
describe('ctx.throw(msg, status)', function(){
|
2013-11-13 17:01:15 +00:00
|
|
|
it('should throw an error', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
try {
|
2013-11-19 01:38:12 +00:00
|
|
|
ctx.throw('name required', 400);
|
2013-11-13 17:01:15 +00:00
|
|
|
} catch (err) {
|
|
|
|
assert('name required' == err.message);
|
|
|
|
assert(400 == err.status);
|
2014-08-06 13:31:55 +00:00
|
|
|
assert(true === err.expose);
|
2013-11-13 17:01:15 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2013-12-20 23:34:16 +00:00
|
|
|
describe('ctx.throw(status, msg)', function(){
|
|
|
|
it('should throw an error', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-12-20 23:34:16 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.throw(400, 'name required');
|
|
|
|
} catch (err) {
|
|
|
|
assert('name required' == err.message);
|
|
|
|
assert(400 == err.status);
|
2014-08-06 13:31:55 +00:00
|
|
|
assert(true === err.expose);
|
2013-12-20 23:34:16 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2013-11-19 01:38:12 +00:00
|
|
|
describe('ctx.throw(status)', function(){
|
2013-11-13 17:01:15 +00:00
|
|
|
it('should throw an error', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
try {
|
2013-11-19 01:38:12 +00:00
|
|
|
ctx.throw(400);
|
2013-11-13 17:01:15 +00:00
|
|
|
} catch (err) {
|
|
|
|
assert('Bad Request' == err.message);
|
|
|
|
assert(400 == err.status);
|
2014-08-06 13:31:55 +00:00
|
|
|
assert(true === err.expose);
|
2013-11-13 17:01:15 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
2014-08-06 13:31:55 +00:00
|
|
|
|
|
|
|
describe('when not valid status', function(){
|
|
|
|
it('should not expose', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2014-08-06 13:31:55 +00:00
|
|
|
|
|
|
|
try {
|
2015-10-05 18:23:47 +00:00
|
|
|
const err = new Error('some error');
|
2014-08-06 13:31:55 +00:00
|
|
|
err.status = -1;
|
|
|
|
ctx.throw(err);
|
|
|
|
} catch(err) {
|
|
|
|
assert('some error' == err.message);
|
|
|
|
assert(!err.expose);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2014-01-04 08:23:39 +00:00
|
|
|
})
|
2014-08-08 19:37:04 +00:00
|
|
|
|
|
|
|
describe('ctx.throw(status, msg, props)', function(){
|
|
|
|
it('should mixin props', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2014-08-08 19:37:04 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.throw(400, 'msg', { prop: true });
|
|
|
|
} catch (err) {
|
|
|
|
assert('msg' == err.message);
|
|
|
|
assert(400 == err.status);
|
|
|
|
assert(true === err.expose);
|
|
|
|
assert(true === err.prop);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
2014-08-09 05:38:54 +00:00
|
|
|
|
|
|
|
describe('when props include status', function(){
|
|
|
|
it('should be ignored', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2014-08-09 05:38:54 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.throw(400, 'msg', {
|
|
|
|
prop: true,
|
|
|
|
status: -1
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
assert('msg' == err.message);
|
|
|
|
assert(400 == err.status);
|
|
|
|
assert(true === err.expose);
|
|
|
|
assert(true === err.prop);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-08-12 20:19:14 +00:00
|
|
|
|
|
|
|
describe('ctx.throw(msg, props)', function(){
|
|
|
|
it('should mixin props', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2014-08-12 20:19:14 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.throw('msg', { prop: true });
|
|
|
|
} catch (err) {
|
|
|
|
assert('msg' == err.message);
|
|
|
|
assert(500 == err.status);
|
|
|
|
assert(false === err.expose);
|
|
|
|
assert(true === err.prop);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('ctx.throw(status, props)', function(){
|
|
|
|
it('should mixin props', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2014-08-12 20:19:14 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.throw(400, { prop: true });
|
|
|
|
} catch (err) {
|
|
|
|
assert('Bad Request' == err.message);
|
|
|
|
assert(400 == err.status);
|
|
|
|
assert(true === err.expose);
|
|
|
|
assert(true === err.prop);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2014-08-12 20:22:33 +00:00
|
|
|
|
|
|
|
describe('ctx.throw(err, props)', function(){
|
|
|
|
it('should mixin props', function(done){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2014-08-12 20:22:33 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.throw(new Error('test'), { prop: true });
|
|
|
|
} catch (err) {
|
|
|
|
assert('test' == err.message);
|
|
|
|
assert(500 == err.status);
|
|
|
|
assert(false === err.expose);
|
|
|
|
assert(true === err.prop);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|