当开发环境为dev, 在id时非正整数的时候,不能正确显示错误信息,终端显示Error 在 lin-validator.js:87:19

来源:5-3 配置文件与在终端显示异常

猴犀利的7号

2019-07-03

写回答

3回答

猴犀利的7号

提问者

2019-07-04

const isHttpException = error instanceof HttpException
const isDev = global.config.environment === 'dev'

if(isDev && !isHttpException){
throw error
}

看来源码,是否结合这样判断 就可以解决bug??

1
1
昨夜秋风起
我也遇到了,和你同样的问题,谢谢
2020-04-07
共1条回复

猴犀利的7号

提问者

2019-07-04

// 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
}


0
0

7七月

2019-07-04

麻烦帖一下代码,Lin-Validator和dev还是prod有什么关系呢?不太明白

0
4
rubyc
const { HttpException } = require("../core/http-exception") const catchError = async (ctx, next) => { try { await next() } catch (error) { // throw error if (error instanceof HttpException) { ctx.body = { msg: error.msg, error_code: error.errorCode, request: `${ctx.method} ${ctx.path}` } ctx.status = error.code } else { // 主要是把环境判断放来这里就解决了,这样避免dev环境下的所有error包括lin-validator的参数校验不通过的error全部被throw,而不能看到参数校验返回的msg if (global.config.environment === 'dev') { console.log('ffffffff'); throw error } ctx.body = { msg: 'we made a mistake! ', error_code: 999, request: `${ctx.method} ${ctx.path}` } } } } module.exports = catchError
2021-12-27
共4条回复

Node.js+Koa2+MySQL打造前后端分离精品项目《旧岛》

理解异步编程本质/培养面向对象思维,独立完成Node.js服务端开发

2223 学习 · 878 问题

查看课程