[英]Hapi/Joi validation error log
I'm developing an API using hapi js and couchbase. 我正在使用hapi js和couchbase开发API。 I'm using log4js to log the errors. 我正在使用log4js来记录错误。
{
// Add a user level
method: 'POST',
path: '/api/v1/userlevel',
config: {
handler: (request, reply) => {
const userlevel = new Userlevel(request.payload);
userlevel.save((err) =>{
if(err) {
return reply({
status: 400,
message: err.message
}).code(400);
// logger.error(err);
}
// logger.debug(reply);
return reply(userlevel).code(201);
});
},
validate: {
payload: {
group_id: Joi.string(),
name: Joi.string(),
status: Joi.string(),
index_id: Joi.number(),
device_id: Joi.string(),
created_at: Joi.string(),
updated_at: Joi.string(),
sys_version: Joi.string()
}
}
}
}
when i'm sending a POST request to this endpoint with invalid data it is showing an error 当我向此端点发送POST请求时,它显示错误
POST request POST请求
{
"group_id" : "test1",
"name" : "test1",
"status":"test1",
"index_id":1,
"device_id":"test1",
"created_at":7,
"updated_at":7,
"sys_version":7
}
error 错误
{"statusCode":400,"error":"Bad Request","message":"child \"created_at\" fails because [\"created_at\" must be a string]","validation":{"source":"payload","keys":["created_at"]}}
I need to log this error message. 我需要记录此错误消息。 I have tried to find the place which this message is generating. 我试图找到这条消息产生的地方。 But i couldn't find it. 但我找不到它。 Please help. 请帮忙。 Thank you in advance. 先感谢您。
If you're willing to migrate to Hapi 17 the following will work. 如果您愿意迁移到Hapi 17,以下内容将起作用。 There may well be a similar Hapi 16 solution if you have a dig through the documentation. 如果您仔细阅读文档,可能会有类似的Hapi 16解决方案。
There's a failAction
callback method you can hook into if a route validation fails, here seems the best place to log your error. 如果路由验证失败,可以使用failAction
回调方法,这里似乎是记录错误的最佳位置。
const config = {
routes: {
validate: {
failAction: async (request, h, err) =>
{
if (err.isJoi)
{
// do something with error
console.log(err.message);
}
throw err;
}
}
}
};
const server = new Hapi.Server(config);
You can achieve this almost the same way with Hapi 16, see below : 你可以用Hapi 16几乎以同样的方式实现这一点,见下文:
server.route({
method: 'GET',
path: '/what/ever,
handler: function(request, reply) {
},
config: {
validate: {
payload: {},
failAction: function(request, reply, source, error) {
// do your stuff here
// you should also reply something
}
}
});
I think better you can give custom error message 我认为你可以提供自定义错误消息
{
method: 'POST',
path: '/api/v1/userlevel',
config: {
handler: (request, reply) => {
const userlevel = new Userlevel(request.payload);
userlevel.save((err) =>{
if(err) {
return reply({
status: 400,
message: err.message
}).code(400);
// logger.error(err);
}
// logger.debug(reply);
return reply(userlevel).code(201);
});
},
validate: {
payload: {
group_id: Joi.string().error(new Error('custom message')),
name: Joi.string().error(new Error('custom message')),
status: Joi.string().error(new Error('custom message')),
index_id: Joi.number().error(new Error('custom message')),
device_id: Joi.string().error(new Error('custom message')),
created_at: Joi.string().error(new Error('custom message')),
updated_at: Joi.string().error(new Error('custom message')),
sys_version: Joi.string().error(new Error('custom message'))
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.