2013-08-21 66 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

谢谢,这帮了我一把! –