.throw() -> http-errors, .assert() -> http-assert
This commit is contained in:
parent
24c70eebbb
commit
2f0fe55ae3
3 changed files with 52 additions and 31 deletions
|
@ -3,6 +3,8 @@
|
||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var createError = require('http-errors');
|
||||||
|
var httpAssert = require('http-assert');
|
||||||
var delegate = require('delegates');
|
var delegate = require('delegates');
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
|
@ -49,6 +51,21 @@ var proto = module.exports = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Similar to .throw(), adds assertion.
|
||||||
|
*
|
||||||
|
* this.assert(this.user, 401, 'Please login!');
|
||||||
|
*
|
||||||
|
* See: https://github.com/jshttp/http-assert
|
||||||
|
*
|
||||||
|
* @param {Mixed} test
|
||||||
|
* @param {Number} status
|
||||||
|
* @param {String} message
|
||||||
|
* @api public
|
||||||
|
*/
|
||||||
|
|
||||||
|
assert: httpAssert,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throw an error with `msg` and optional `status`
|
* Throw an error with `msg` and optional `status`
|
||||||
* defaulting to 500. Note that these are user-level
|
* defaulting to 500. Note that these are user-level
|
||||||
|
@ -61,31 +78,16 @@ var proto = module.exports = {
|
||||||
* this.throw(new Error('invalid'), 400);
|
* this.throw(new Error('invalid'), 400);
|
||||||
* this.throw(400, new Error('invalid'));
|
* this.throw(400, new Error('invalid'));
|
||||||
*
|
*
|
||||||
|
* See: https://github.com/jshttp/http-errors
|
||||||
|
*
|
||||||
* @param {String|Number|Error} err, msg or status
|
* @param {String|Number|Error} err, msg or status
|
||||||
* @param {String|Number|Error} [err, msg or status]
|
* @param {String|Number|Error} [err, msg or status]
|
||||||
* @param {Object} [props]
|
* @param {Object} [props]
|
||||||
* @api public
|
* @api public
|
||||||
*/
|
*/
|
||||||
|
|
||||||
throw: function(msg, status, props){
|
throw: function(){
|
||||||
if ('object' == typeof status && !(status instanceof Error)) {
|
throw createError.apply(null, arguments);
|
||||||
props = status;
|
|
||||||
status = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('number' == typeof msg) {
|
|
||||||
var tmp = msg;
|
|
||||||
msg = status || http.STATUS_CODES[tmp];
|
|
||||||
status = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
props = props || {};
|
|
||||||
|
|
||||||
var err = msg instanceof Error ? msg : new Error(msg);
|
|
||||||
for (var key in props) err[key] = props[key];
|
|
||||||
err.status = status || err.status || 500;
|
|
||||||
err.expose = 'number' == typeof err.status && http.STATUS_CODES[err.status] && err.status < 500;
|
|
||||||
throw err;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
26
package.json
26
package.json
|
@ -18,26 +18,28 @@
|
||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"escape-html": "~1.0.1",
|
|
||||||
"statuses": "~1.0.1",
|
|
||||||
"accepts": "~1.1.0",
|
"accepts": "~1.1.0",
|
||||||
"type-is": "~1.5.0",
|
|
||||||
"mime-types": "~2.0.0",
|
|
||||||
"media-typer": "~0.3.0",
|
|
||||||
"on-finished": "~2.1.0",
|
|
||||||
"co": "~3.1.0",
|
"co": "~3.1.0",
|
||||||
"content-disposition": "~0.1.0",
|
"content-disposition": "~0.1.0",
|
||||||
"debug": "*",
|
|
||||||
"fresh": "~0.2.1",
|
|
||||||
"koa-compose": "~2.3.0",
|
|
||||||
"koa-is-json": "~1.0.0",
|
|
||||||
"cookies": "~0.5.0",
|
"cookies": "~0.5.0",
|
||||||
|
"debug": "*",
|
||||||
"delegates": "0.0.3",
|
"delegates": "0.0.3",
|
||||||
"destroy": "~1.0.3",
|
"destroy": "~1.0.3",
|
||||||
"error-inject": "~1.0.0",
|
"error-inject": "~1.0.0",
|
||||||
"vary": "~1.0.0",
|
"escape-html": "~1.0.1",
|
||||||
|
"fresh": "~0.2.1",
|
||||||
|
"http-assert": "~1.0.1",
|
||||||
|
"http-errors": "~1.2.0",
|
||||||
|
"koa-compose": "~2.3.0",
|
||||||
|
"koa-is-json": "~1.0.0",
|
||||||
|
"media-typer": "~0.3.0",
|
||||||
|
"mime-types": "~2.0.0",
|
||||||
|
"on-finished": "~2.1.0",
|
||||||
|
"only": "0.0.2",
|
||||||
"parseurl": "~1.3.0",
|
"parseurl": "~1.3.0",
|
||||||
"only": "0.0.2"
|
"statuses": "~1.0.1",
|
||||||
|
"type-is": "~1.5.0",
|
||||||
|
"vary": "~1.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"istanbul-harmony": "~0.3.0",
|
"istanbul-harmony": "~0.3.0",
|
||||||
|
|
17
test/context/assert.js
Normal file
17
test/context/assert.js
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
|
||||||
|
var context = require('../context');
|
||||||
|
var assert = require('assert');
|
||||||
|
|
||||||
|
describe('ctx.assert(value, status)', function(){
|
||||||
|
it('should throw an error', function(){
|
||||||
|
var ctx = context();
|
||||||
|
|
||||||
|
try {
|
||||||
|
ctx.assert(false, 404);
|
||||||
|
throw new Error('asdf');
|
||||||
|
} catch (err) {
|
||||||
|
assert(404 == err.status);
|
||||||
|
assert(err.expose);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in a new issue