2014-09-02 107 views
0

我想用Sentry + Raven檢測獨立Python腳本中的錯誤。
我試圖配置它並raven test ...正在工作。
後來我把這個在腳本的頂部:Sentry只顯示<unknown>:無錯誤

from raven import Client 
client = Client('http://[email protected]/1') 
client.captureException() 

的異常後產生於這樣的:

import django 
django.setup() 
from django.conf import settings 

而且我希望看到這個錯誤的實際堆棧:

ImportError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named 'settings' 

但我在哨兵看到的全部是

Useless message

這是完全沒用的。

我該如何改變這個以獲得正常的回溯?

回答

3

您誤會了client.captureException()的工作原理,它不是配置參數。您可以使用它,當你捕獲異常,這將捕獲的異常類型和消息:

try: 
    f = open('oogah-boogah.txt') 
except IOError: 
    client.captureException() 
    # do something here 

爲了捕捉可能在一個代碼塊中產生任何異常,您可以使用capture_exceptions

@client.capture_exceptions 
def load_django(): 
    import django 
    django.setup() 
    from django.conf import settings 

Yes you're right, but is there a way to catch an exception not wrapping a block of code in a try-except. I can see the error in a terminal, can I see it in Sentry?

有一個默認的異常處理 - 在未捕獲的異常,這種默認的處理程序將其捕獲並顯示異常。這是你在終端上看到的。

生成此輸出的函數是sys.excepthook,默認情況下它將輸出到stderr

所以,爲了讓你趕上所有例外全球範圍內,你必須創建一個全球性的異常處理程序或您自己的函數映射到sys.excepthook

我強烈建議不要這樣做,但因爲你不知道它可能有什麼其他副作用。

+0

是的你是對的,但是有沒有辦法在try-except中捕獲一個不包含代碼塊的異常。我可以在終端看到錯誤,我可以在Sentry中看到它嗎? – Sergey 2014-09-02 10:54:56