2013-08-21 63 views
1

我想處理jira-python異常,但是我的嘗試,除了似乎沒有抓住它。我還需要添加更多行以便能夠發佈。所以他們在那裏,線條。捕捉jira-python異常

try: 
    new_issue = jira.create_issue(fields=issue_dict) 
    stdout.write(str(new_issue.id)) 
except jira.exceptions.JIRAError: 
    stdout.write("JIRAError") 
    exit(1) 

這裏是引發異常的代碼:

import json 


class JIRAError(Exception): 
    """General error raised for all problems in operation of the client.""" 
    def __init__(self, status_code=None, text=None, url=None): 
     self.status_code = status_code 
     self.text = text 
     self.url = url 

    def __str__(self): 
     if self.text: 
      return 'HTTP {0}: "{1}"\n{2}'.format(self.status_code, self.text, self.url) 
     else: 
      return 'HTTP {0}: {1}'.format(self.status_code, self.url) 


def raise_on_error(r): 
    if r.status_code >= 400: 
     error = '' 
     if r.text: 
      try: 
       response = json.loads(r.text) 
       if 'message' in response: 
        # JIRA 5.1 errors 
        error = response['message'] 
       elif 'errorMessages' in response and len(response['errorMessages']) > 0: 
        # JIRA 5.0.x error messages sometimes come wrapped in this array 
        # Sometimes this is present but empty 
        errorMessages = response['errorMessages'] 
        if isinstance(errorMessages, (list, tuple)): 
         error = errorMessages[0] 
        else: 
         error = errorMessages 
       elif 'errors' in response and len(response['errors']) > 0: 
        # JIRA 6.x error messages are found in this array. 
        error = response['errors'] 
       else: 
        error = r.text 
      except ValueError: 
       error = r.text 
     raise JIRAError(r.status_code, error, r.url) 
+1

我對這個庫最大的問題是沒有人似乎知道如何使用它!你有沒有得到這個解決? –

回答

2

我可能是錯的,但看起來像你趕上jira.exceptions.JIRAError,同時提高JIRAError - 這些都是不同的類型。您需要刪除except聲明中的「jira.exceptions.」部分,或者改爲提起jira.exceptions.JIRAError

1

也許這是顯而易見的,這就是爲什麼你沒有在你的代碼粘貼,以防萬一,你必須

from jira.exceptions import JIRAError 
在你的代碼的權利

地方?

沒有足夠的評論聲望,所以我將它添加回答@arynhard: 我發現文檔非常輕便,特別是在示例方面,您可能會發現這個回購中的腳本很有用,因爲它們是所有這些都在某種程度上利用了jira-python。 https://github.com/eucalyptus/jira-scripts/

1

好吧,這是一個很舊的,但我面臨同樣的問題,這個網頁仍然顯示出來。

下面是我如何捕獲Exception,我使用了Exception對象。

try: 
     issue = jira.issue('jira-1') 
except Exception as e: 
     if 'EXIST' in e.text: 
       print 'The issue does not exist' 
       exit(1) 

問候

2

我知道我不回答這個問題,但我覺得我有必要提醒人們,可以通過代碼混淆(像我一樣)...... 也許你正在嘗試編寫你自己的jira-python版本還是舊版本?

在任何情況下,here鏈接到JIRA-Python代碼爲JIRAError 類和here

的列表,以捕獲從該包中的例外,我使用下面的代碼

from jira import JIRA, JIRAError 
try: 
    ... 
except JIRAError as e: 
    print e.status_code, e.text 
+0

謝謝,這幫了我一把! –