2011-04-08 160 views
0

我正在開發一些fb應用程序,使用python-sdk。 有時候,我得到一個「未捕獲異常」,看起來像:如何捕捉「未捕獲的異常」?

Uncaught exception GET… 
… 
… 
GraphAPIError: Error validating access token. 

我知道這個異常的原因,以及如何解決這個問題(更新OAuth令牌)。

但我怎麼能抓住它?

傳統try: .. except:不這樣做。

尋找一個解決方案,而不是黑客的SDK代碼。

完整的錯誤輸出,要求在註釋:

[E 110407 20:44:20 web:900] Uncaught exception GET /fl?access_token=122385271170470|2.teD_D9DV6gegmpgkbZI_Hg__.3600.1302206400-1592301191|GuXEs4YTqsCPDNFU4fhbWuwlvOQ (127.0.0.1) 
    HTTPRequest(protocol='http', host='XX.XX.100.66', method='GET', uri='/fl?access_token=122385271170470|2.teD_D9DV6gegmpgkbZI_Hg__.3600.1302206400-1592301191|GuXEs4YTqsCPDNFU4fhbWuwlvOQ', version='HTTP/1.0', remote_ip='127.0.0.1', body='', headers={'Accept-Language': 'en-us,en;q=0.5', 'Accept-Encoding': 'gzip,deflate', 'X-Forwarded-For': '109.253.54.46', 'Host': 'XX.XX.100.66', 'Accept': 'application/json, text/javascript, */*', 'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.16) Gecko/20110323 Ubuntu/10.10 (maverick) Firefox/3.6.16', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Connection': 'close', 'X-Requested-With': 'XMLHttpRequest', 'X-Real-Ip': 'XX.XX.54.46', 'Referer': 'http://XX.XX.100.66/'}) 
    Traceback (most recent call last): 
     File "/usr/local/lib/python2.6/dist-packages/tornado-1.2-py2.6.egg/tornado/web.py", line 861, in _execute 
     getattr(self, self.request.method.lower())(*args, **kwargs) 
     File "/usr/local/lib/python2.6/dist-packages/tornado-1.2-py2.6.egg/tornado/web.py", line 941, in wrapper 
     return method(self, *args, **kwargs) 
     File "/home/blabla/src/python-sdk/examples/tornado/tornading.py", line 199, in get 
     self.render("json_output.html", json = json.dumps({"error":"login"})) 
     File "/usr/local/lib/python2.6/dist-packages/tornado-1.2-py2.6.egg/tornado/web.py", line 419, in render 
     html = self.render_string(template_name, **kwargs) 
     File "/usr/local/lib/python2.6/dist-packages/tornado-1.2-py2.6.egg/tornado/web.py", line 520, in render_string 
     current_user=self.current_user, 
     File "/usr/local/lib/python2.6/dist-packages/tornado-1.2-py2.6.egg/tornado/web.py", line 683, in current_user 
     self._current_user = self.get_current_user() 
     File "/home/blabla/src/python-sdk/examples/tornado/tornading.py", line 109, in get_current_user 
     user = graph.get_object("me") 
     File "/home/blabla/src/python-sdk/src/facebook.py", line 88, in get_object 
     return self.request(id, args) 
     File "/home/blabla/src/python-sdk/src/facebook.py", line 179, in request 
     response["error"]["message"]) 
    GraphAPIError: Error validating access token. 
[E 110407 20:44:20 web:1235] 500 GET /fl?access_token=122385271170470|2.teD_D9DV6gegmpgkbZI_Hg__.3600.1302206400-1592301191|GuXEs4YTqsCPDNFU4fhbWuwlvOQ (127.0.0.1) 2714.20ms 

嘗試/除非代碼:
try:
       graph = facebook.GraphAPI(access_token)
except:
       assert False, 'cougth it'

+1

你確定你沒有捕捉(併吞咽)在其他地方的例外嗎? – 2011-04-08 09:35:43

+0

我不知道你是否可以在Python中做到這一點,但我會嘗試調試這與調試器設置爲停止何時何地拋出異常,並按照其路徑向上檢查堆棧。 – 2011-04-08 09:41:10

+0

當然,我無法抓住它,腳本停止工作。我是Python新手,第一次遇到這種異常。我想我可以通過破解SDK代碼來解決它,但是我試圖避免它,如果可能的話。 – 2011-04-08 09:44:06

回答

0

可能異常被另一個線程產卵拋出該API,檢查其源代碼。

1
+0

在發佈問題之前,我檢查了這一點。我需要處理一個異常,而不是調試它。 – 2011-04-08 14:07:13