2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-12 04:59:30 +00:00
|
|
|
const context = require('../helpers/context');
|
2015-10-05 18:23:47 +00:00
|
|
|
const assert = require('assert');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(msg)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should set .status to 500', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.status, 500);
|
|
|
|
assert.equal(err.expose, false);
|
2013-11-13 17:01:15 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(err)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should set .status to 500', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.status, 500);
|
|
|
|
assert.equal(err.message, 'test');
|
|
|
|
assert.equal(err.expose, false);
|
2014-01-04 08:23:39 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-01-04 08:23:39 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(err, status)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should throw the error and set .status', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.status, 422);
|
|
|
|
assert.equal(err.message, 'test');
|
|
|
|
assert.equal(err.expose, true);
|
2014-01-04 08:23:39 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-01-04 08:23:39 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(status, err)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should throw the error and set .status', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.status, 422);
|
|
|
|
assert.equal(err.message, 'test');
|
|
|
|
assert.equal(err.expose, true);
|
2014-01-04 08:23:39 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-01-04 08:23:39 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(msg, status)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should throw an error', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.message, 'name required');
|
|
|
|
assert.equal(err.status, 400);
|
|
|
|
assert.equal(err.expose, true);
|
2013-11-13 17:01:15 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(status, msg)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should throw an error', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.message, 'name required');
|
|
|
|
assert.equal(400, err.status);
|
|
|
|
assert.equal(true, err.expose);
|
2013-12-20 23:34:16 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-12-20 23:34:16 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(status)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should throw an error', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.message, 'Bad Request');
|
|
|
|
assert.equal(err.status, 400);
|
|
|
|
assert.equal(err.expose, true);
|
2013-11-13 17:01:15 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-08-06 13:31:55 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when not valid status', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should not expose', () => {
|
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);
|
2015-10-12 20:36:41 +00:00
|
|
|
} catch (err) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.message, 'some error');
|
|
|
|
assert.equal(err.expose, false);
|
2014-08-06 13:31:55 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-08-08 19:37:04 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(status, msg, props)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should mixin props', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.message, 'msg');
|
|
|
|
assert.equal(err.status, 400);
|
|
|
|
assert.equal(err.expose, true);
|
|
|
|
assert.equal(err.prop, true);
|
2014-08-08 19:37:04 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-08-09 05:38:54 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when props include status', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should be ignored', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.message, 'msg');
|
|
|
|
assert.equal(err.status, 400);
|
|
|
|
assert.equal(err.expose, true);
|
|
|
|
assert.equal(err.prop, true);
|
2014-08-09 05:38:54 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-08-12 20:19:14 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(msg, props)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should mixin props', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.message, 'msg');
|
|
|
|
assert.equal(err.status, 500);
|
|
|
|
assert.equal(err.expose, false);
|
|
|
|
assert.equal(err.prop, true);
|
2014-08-12 20:19:14 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-08-12 20:19:14 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(status, props)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should mixin props', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.message, 'Bad Request');
|
|
|
|
assert.equal(err.status, 400);
|
|
|
|
assert.equal(err.expose, true);
|
|
|
|
assert.equal(err.prop, true);
|
2014-08-12 20:19:14 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-08-12 20:22:33 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.throw(err, props)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should mixin props', () => {
|
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) {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.message, 'test');
|
|
|
|
assert.equal(err.status, 500);
|
|
|
|
assert.equal(err.expose, false);
|
|
|
|
assert.equal(err.prop, true);
|
2014-08-12 20:22:33 +00:00
|
|
|
}
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|