捕获jira python异常

捕获jira python异常,python,jira,python-3.3,python-jira,Python,Jira,Python 3.3,Python Jira,我正在尝试处理jira python异常,但我的尝试,except似乎没有捕获到它。我还需要添加更多的行,以便能够张贴这个。这就是他们的台词 try: new_issue = jira.create_issue(fields=issue_dict) stdout.write(str(new_issue.id)) except jira.exceptions.JIRAError: stdout.write("JIRAError") exit(1) 以下是引发异常的

我正在尝试处理jira python异常,但我的尝试,except似乎没有捕获到它。我还需要添加更多的行,以便能够张贴这个。这就是他们的台词

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)

我可能错了,但看起来您正在捕获
jira.exceptions.JIRAError
,同时引发
JIRAError
-这些是不同的类型。您需要从
exception
语句中删除“
jira.exceptions.
”部分,或者提出
jira.exceptions.JIRAError

也许这很明显,这就是为什么您的代码粘贴中没有它的原因,以防万一,您确实有

from jira.exceptions import JIRAError
在你代码的某个地方,对吗

没有足够的评论声誉,因此我将为@arynhard添加答案: 我发现文档非常简单,特别是在示例方面,您可能会发现这个repo中的脚本非常有用,因为它们都以某种方式利用了jirapython。

好的,这是一个非常旧的页面,但我遇到了同样的问题,并且这个页面仍然显示出来

下面是我捕获异常的方法,我使用了异常对象

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

关于

我知道我没有回答这个问题,但我觉得我需要警告那些可能会被代码弄糊涂的人(就像我那样)。。。 也许您正在尝试编写自己的jira python版本,或者它是一个旧版本

在任何情况下,JIRAError类的jirapython代码的链接 以及代码列表

为了捕获该包中的异常,我使用以下代码

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

这个图书馆最大的问题是似乎没有人知道如何使用它!你解决过这个问题吗?谢谢,这帮了我的忙!