处理控制器在涡轮齿轮 2 中缺少控制器参数

发布于 2024-07-26 01:14:39 字数 433 浏览 2 评论 0原文

假设我有一个像这样的控制器方法:

@expose()
def search(self, title):
    return dict()

转到 http://site/search/ 将导致异常抛出:TypeError:search() 恰好需要 2 个参数(给定 1 个)。

这个错误是合乎逻辑的,但我宁愿更优雅地处理它。 使用 *args 或 **kwargs 是避免我似乎无法捕获的错误的唯一方法吗?

编辑:我想我总是可以使用 title=None,但是太多可能会变得丑陋......

无论如何,有没有一种方法可以更优雅地捕获异常和/或处理参数不匹配?

谢谢

Suppose I have a controller method like so:

@expose()
def search(self, title):
    return dict()

Going to http://site/search/ will cause an exception to be thrown: TypeError: search() takes exactly 2 arguments (1 given).

The error is logical, but I'd rather handle it more gracefully. Is using *args or **kwargs the only way to avoid an error that I don't even seem to be able to catch?

EDIT: I guess I could always use title=None, but too much of that could get ugly...

Anyway, is there a way to catch the exception and/or handle argument mismatches more gracefully?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2024-08-02 01:14:39

因指定“不兼容”控制器方法签名而向您抛出的异常仅发生在调试/开发模式下。
您不需要在生产环境中更优雅地处理它,因为一旦禁用开发模式,控制器方法在缺少必要参数时会发送 HTTP 500 错误。

您可能需要考虑development.ini 中的相应设置:

# WARNING: *THE LINE BELOW MUST BE UNCOMMENTED ON A PRODUCTION ENVIRONMENT*
# Debug mode will enable the interactive debugging tool, allowing ANYONE to
# execute malicious code after an exception is raised.
set debug = false

我希望这是您的问题。

如果您仍然希望控制器完成其工作,即使它缺少重要参数,您必须定义默认值,否则控制器无论如何都无法正常工作。
您最好问自己的问题是:您只是想要更好的错误消息,还是希望控制器能够完成其任务。 在后一种情况下,指定默认参数是最佳实践,为每个方法指定 *args 和 **kwargs 只是为了让客户不会收到错误,这是我的选择中非常丑陋的黑客行为。

如果您想更改这些错误的显示,请参阅 /controllers/error.py

希望这有帮助,

汤姆

The exception thrown at you for specifying an "incompatible" controller method signature only happens in debug / development mode.
You dont need to handle it more gracefully in a production environment, because once you disable development mode, controller methods send an HTTP 500 Error when they lack essential parameters.

You might want to consider the respective settings in your development.ini:

# WARNING: *THE LINE BELOW MUST BE UNCOMMENTED ON A PRODUCTION ENVIRONMENT*
# Debug mode will enable the interactive debugging tool, allowing ANYONE to
# execute malicious code after an exception is raised.
set debug = false

I hope this was your question.

In the case that you still want the controller do its work, even though its lacks important parameters, you must define default values, else the controller cannot do its work properly anyway.
The question you better ask yourself is: Do you simply want a nicer error message, or do you want the controller to be able to do its task. In the latter case, specifying default parameters is best practise, *args and **kwargs for each method just so the customer doesnt get an error is a very ugly hack in my option.

If you want to change the display of these errors refer to /controllers/error.py

Hope this helped,

Tom

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文