捕获jira python异常

2024-10-05 10:12:10 发布

您现在位置: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)

Tags: textinselfurlifresponsedefstatus
3条回答

也许这是显而易见的,这就是为什么你没有在你的代码粘贴,以防万一,你有

from jira.exceptions import JIRAError

你密码里的某个地方对吧?

没有足够的声誉发表评论,因此我将为@arynhard添加答案: 我发现这些文档非常轻巧,特别是在示例方面,您可能会发现这个repo中的脚本非常有用,因为它们都以某种方式利用了jira python。 https://github.com/eucalyptus/jira-scripts/

我可能是错的,但是看起来你是在抓捕jira.exceptions.JIRAError,而在抓捕JIRAError-这是不同的类型。您需要从except语句中删除“jira.exceptions.”部分,或者改为提升jira.exceptions.JIRAError

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

在任何情况下,here指向JIRAError类的jira python代码的链接 以及here代码列表

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

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

相关问题 更多 >

    热门问题