如何在Sails.js中引发自定义错误消息 [英] How to raise custom error message in Sails.js

查看:111
本文介绍了如何在Sails.js中引发自定义错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢Sails.js(0.9.13)和一般的节点风格编程。任何帮助赞赏。下列示例来自UserController:

  module.exports = {

save:function(req ,res){

var u = {
firstName:req.param('firstName'),
lastName:req.param('lastName'),
电子邮件:req.param('email'),
密码:req.param('password'),
};

User.create(u).done(function(err,user){
if(err){
if(err.code =='ER_DUP_ENTRY'){
返回新的错误('重复:电子邮件地址已被使用','UserController');
}
console.log(err);
return;
}
console.log(user);
});
}
}

所以我试图捕获'ER_DUP_ENTRY'错误代码并返回一个自定义错误消息,但我没有运气写的。我该怎么做这个?

解决方案

如果您打算将错误返回给客户端,有几种方法可以去,他们从 res 对象作为参数传递给您的控制器操作。所有最新的Sails版本都有一些基本的错误反应,这样您就可以(例如):

  res.serverError(err)

而不是返回新错误显示具有500状态代码的常规错误页面。对于不需要HTML的只有JSON的API调用,它将发送一个具有状态代码和错误的简单对象。还有:

  res.notFound()// 404 response 
res.forbidden()// 403 response
res.badRequest()// 400响应

对于您的重复条目错误,您可能想要更多的定制。由于您的示例来自表单帖子,最佳做法是将错误实际保存为Flash消息并重定向到表单页面,例如:

  req.flash('error','Duplicate email address'); 
return res.redirect('/ theView');

然后在您的表单视图中,使用以下内容显示错误:

 <%if(req.session.flash&& req.session.flash.error){%><%= req.flash 'error')%><%}%> 

另外,您可以从控制器显示自定义错误视图:

  return res.view('duplicateEmail'); 

如果您要回应API通话,则需要发送状态码和(可能)一个JSON对象:

  return res.json(409,{error:'Email address in use'});关键是,而不是返回(或抛出)一个错误 



对象在您的控制器代码中,您想使用 res 对象以某种方式响应请求。


I am very new to Sails.js (0.9.13) and to node style programming in general. Any help appreciated. The collowing example is from a UserController:

module.exports = {

    save: function (req, res) {

        var u = {    
            firstName: req.param('firstName'),
            lastName: req.param('lastName'),
            email: req.param('email'),
            password: req.param('password'),
        };

        User.create(u).done(function(err,user){
            if(err) {       
                if( err.code == 'ER_DUP_ENTRY' ) {
                    return new Error('Duplicate: email address already in use.','UserController');
                }
                console.log( err );
                return;
            } 
            console.log( user );
        });
   }
}

So I am trying to capture the 'ER_DUP_ENTRY' error code and return a custom error message, but I am having no luck as written. How exactly should I be doing this?

解决方案

If what you're intending is to return an error to the client, there are several ways to go, but all of them start with the res object that is passed as an argument to your controller action. All of the latest builds of Sails have a few basic error responses baked into that object, so you can do (for example):

res.serverError(err)

instead of return new Error to show a general error page with a 500 status code. For JSON-only API calls that don't expect HTML, it'll send a simple object with the status code and the error. There's also:

res.notFound() // 404 response
res.forbidden() // 403 response
res.badRequest() // 400 response

For your "duplicate entry" error, you'll probably want something more customized. Since your example is coming from a form post, the best practice would be to actually save the error as a flash message and redirect back to the form page, for example:

req.flash('error', 'Duplicate email address');
return res.redirect('/theView');

and then in your form view, display errors using something like:

<% if (req.session.flash && req.session.flash.error) { %><%=req.flash('error')%><% } %>

As an alternative, you could just display a custom error view from your controller:

return res.view('duplicateEmail');

If you're responding to an API call, you'll want to send a status code and (probably) a JSON object:

return res.json(409, {error: 'Email address in use'});

The point is, rather than returning (or throwing) an Error object in your controller code, you want to use the res object to respond to the request in some way.

这篇关于如何在Sails.js中引发自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆