Meteor:自定义来自允许和拒绝规则的错误消息 [英] Meteor: Customizing errors messages from allow and deny rules

查看:55
本文介绍了Meteor:自定义来自允许和拒绝规则的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 Meteor 中标准化来自允许和拒绝规则的错误消息.而不是在客户端的回调中定义错误,我希望服务器以正确的错误消息进行响应,这样我只需定义一次.来自关于允许/拒绝的文档:

I'm looking to standardize the error messages from allow and deny rules in Meteor. Rather than defining the errors in the callback on the client I'd like the server to respond with the correct error message so that I only have to define them once. From the docs on allow/deny:

如果函数认为应该允许操作,它们应该返回 true.否则他们应该返回 false,或者什么都不返回(未定义).

The functions should return true if they think the operation should be allowed. Otherwise they should return false, or nothing at all (undefined).

但是,如果我在拒绝函数中抛出错误,错误就会发送给用户.IE.拒绝:

However, if I throw an error in a deny function that Error gets sent down to the user. I.e. in a deny:

throw new Meteor.Error(404, "Not the owner");

在客户端:

SomeCollection.remove(someId, function(err, result) {
  console.log(err.message); // Outputs "Not the owner"
});

似乎是在整个网站上显示相同错误的简单方法.尽管文档明确指出我们应该在拒绝/允许中返回真/假,但这是一种有效的方法吗?

Seems like a simple way to display the same errors all over the site. Is this a valid way to go, even though the documentation clearly states that we should return true/false in a deny/allow?

推荐答案

来自文档的这一部分:

http://docs.meteor.com/#meteor_error

前三句:

If you want to return an error from a method, throw an exception. Methods can throw any kind of exception. But Meteor.Error is the only kind of error that a server will send to the client. 

我觉得你做对了.不过我想知道,你的用户界面不应该永远不允许用户出现这样的消息吗?除了哎呀,你不应该被允许这样做.恢复你的行动."以防出现意外允许未经授权的查询通过的错误.

It sounds to me like you're doing it right. I have to wonder though, shouldn't your UI never allow the user to have such a message come up? Other than "Oops, you shouldn't have been allowed to do that. Reverting your action." in case of a bug that accidentally allows an unauthorized query to go through.

这篇关于Meteor:自定义来自允许和拒绝规则的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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