在我的sails.js应用程序模型中,我想从生命周期回调中发送“错误请求”错误代码400。这是我的beforeCreate
函数:
beforeCreate: function (values, next) {
if (!values.email) {
next();
} else{
var err = {
httpCode: 400,
message: 'email not defined.'
};
return next(err);
}
}
不发送400错误,而是发送500错误。这是输出:
error: Sending 500 ("Server Error") response:
Error (E_UNKNOWN) :: Encountered an unexpected error
at new WLError (/app/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:25:15)
at duckType (/app/node_modules/sails/node_modules/waterline/lib/waterline/error/index.js:66:10)
at errorify (/app/node_modules/sails/node_modules/waterline/lib/waterline/error/index.js:39:10)
at wrappedCallback (/app/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:325:15)
at callback.error (/app/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:42:31)
at _switch (/app/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:56:28)
at /app/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:73:23
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:726:13
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:264:21
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:44:16
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:723:17
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:167:37
at /app/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:73:23
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:726:13
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:264:21
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:44:16
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:723:17
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:167:37
at /app/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:35:27
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
Details: { httpCode: 400, message: 'email not defined.' }
我如何实现目标?
答案 0 :(得分:1)
所以你可以看到你收到的错误的最后一行是
详细信息:{httpCode:400,消息:'电子邮件未定义。' }
所以当我们在sails的生命周期回调中发送一个错误对象时,waterline将自己的实现包装在我们发送的错误上
在响应中我们得到一个给定格式的字符串
详细信息:{httpCode:400,消息:'电子邮件未定义。' } //这是字符串
此字符串存在于错误对象
的详细信息属性中所以
假设你的生命周期回调是
beforeValidate: function(values,cb){
cb(JSON.stringify({ httpCode: 400,
code: 997,
message: 'username already exists',
context: 'user create',
custom: true }));
}
尝试对要发送的错误对象进行字符串化
在控制器中,您可以将此错误称为字符串,格式为
详细信息:{您定义的错误对象}
现在在控制器中
//在回调或承诺内容中收到此错误对象
if(error.hasOwnProperty('details')){
var indexStart=error.details.indexOf('{');
//this is where we are extracting the error code that we set inside lifecycle callback
var data =error.details.substring(indexStart);
res.json(500,JSON.parse(data));
}else{
res.json(500,{message : "your message",status : 'your status',data : error});
}