【发布时间】:2020-11-20 01:06:29
【问题描述】:
背景
- fastify
- json 架构
- ajv
问题
当我将 setErrorHandler 添加到我的项目/index.js 时,它不起作用。
require('module-alias/register')
const Fastify = require('fastify')
const PORT = process.env.PORT || 3000
const sequelize = require('./orm')
const swagger = require('./config').swagger
const localize = require('ajv-i18n')
const app = Fastify({
logger: {
prettyPrint: true
},
ajv: {
customOptions: {allErrors: true, jsonPointers: true },
plugins: [
require('ajv-errors')
]
}
})
app.register(require('fastify-sensible'))
app.register(require('fastify-swagger'), swagger)
app.register(require('./plugin/systemlogs'))
app.register(require('./plugin/authenticate')).then(()=>{
const routes = require('./routes')
routes(app).forEach((route, index) => {
app.route(route)
})
})
app.setErrorHandler((error,request,reply)=>{
if (error.validation) {
localize.ru(error.validation)
reply.status(400).send(error.validation)
return
}
reply.send(error)
})
const start = async () => {
try {
await sequelize.sync({})
app.log.info('database sync correctly')
await app.listen(PORT, '0.0.0.0')
app.swagger()
} catch (err) {
app.log.error(err)
process.exit(1)
}
}
start()
问题
我想用ajv i18n把错误转成中文,我该怎么办?我这样做,但它不起作用 我如何在 fastify 中使用 ajv-i18n? 我应该在哪里添加 setErrorHandler?
【问题讨论】:
-
如果有的话,你能添加你的错误模式吗?它们被
app.route(route)隐藏了