// exception.js
const catchError = async (ctx,next) => {
try {
await next()
} catch(error) {
if(global.config.environment === 'dev'){
throw error
}
if(isHttpException){
ctx.body = {
msg: error.msg,
error_code: error.errorCode,
request_url: `${ctx.method} ${ctx.path}`,
}
ctx.status = error.code
}else {
ctx.body = {
msg: '未知错误',
error_code: 999,
request_url: `${ctx.method} ${ctx.path}`,
}
ctx.status = 500
}
}
}
// lin-validator.js
validate(ctx, alias = {}) {
this.alias = alias
let params = this._assembleAllParams(ctx)
this.data = cloneDeep(params)
this.parsed = cloneDeep(params)
const memberKeys = findMembers(this, {
filter: this._findMembersFilter.bind(this)
})
const errorMsgs = []
// const map = new Map(memberKeys)
for (let key of memberKeys) {
const result = this._check(key, alias)
if (!result.success) {
errorMsgs.push(result.msg)
}
}
if (errorMsgs.length != 0) {
throw new ParameterException(errorMsgs) // 这里会触发error
}
ctx.v = this
return this
}