django信号:默默地失败?任何更好的调试错误的方法? [英] django signals: fail silently? any better way of debugging mistakes?

查看:231
本文介绍了django信号:默默地失败?任何更好的调试错误的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎django信号有一个失败的默认范例。

It seems that django signals has a "fail silently" paradigm.

当我在信号功能中发生小的拼写错误时,例如: -


When I make a small spelling error in my signals function, for example:-

def new_users_handler(send, user, response, details, **kwargs):
    print "new_users_handler function executes"
    user.is_new = True
    if user.is_new:
        if "id" in response:
            from urllib2 import urlopen, HTTPError
            from django.template.defaultfilters import slugify
            from django.core.files.base import ContentFile

            try:
                url = None
                if sender == FacebookBackend:
                    url = "http://graph.facebook.com/%s/picture?type=large" \
                            % response["id"]
                elif sender == google.GoogleOAuth2Backend and "picture" in response:
                    url = response["picture"]
    ......
    socialauth_registered.connect(new_users_handler, sender=None)

我使用发送作为我的参数,而不是发件人,我没有真正得到任何有用的错误信息或调试信息在我的devserver stdout。

I use "send" as my argument instead of "sender", I don't really get any useful error message or debug information in my devserver stdout.

有一个很好的方法来确保信号故障/错误消息显示得很清楚?

Is there a good way of making sure that signal failures/error messages get shown loud and clear?

在上面的例子中,如果有一个正确的错误信息告诉我

In my example above, this could have been a "5 minutes" fix if there was a proper error message telling me that

name "sender" is not defined

而是没有任何错误消息,我在代码库中找到无处不在,试图找出为什么我的信号不被调用...不是很酷。

but instead, there wasn't any error messages and I was looking everywhere in my code base to try to figure out why my signals isn't getting called... not cool.

任何建议欢迎!

推荐答案

不完全是你要求的,但你的问题可能也已使用静态分析器(如 pyflakes 解决。

Not quite what you are asking for, but your problem could also have been solved with a static analyser like pyflakes.

从pypi:


Pyflakes是分析Python程序并检测各种错误。它通过解析源文件而不是导入它,因此可以安全地使用具有副作用的模块。

Pyflakes is program to analyze Python programs and detect various errors. It works by parsing the source file, not importing it, so it is safe to use on modules with side effects. It's also much faster.

示例输出:

tmp.py:9: 'ContentFile' imported but unused
tmp.py:13: undefined name 'sender'

我将其整合到我的编辑器(vim,但我也在其他几个人看过),突出显示我的错字,或者保存。

I have it integrated into my editor (vim, but i've also seen it in several others), highlighting my typos as i go, or on save.

这篇关于django信号:默默地失败?任何更好的调试错误的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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