编程语言
首页 > 编程语言> > 捕获jira-python异常

捕获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)

解决方法:

我知道我没有回答这个问题,但我觉得我需要提醒那些可能会被代码混淆的人(正如我所做的那样)……
也许你正在尝试编写自己的jira-python版本,或者它是旧版本?

在任何情况下,here链接到JIRAError类的jira-python代码
here代码列表

要从该包中捕获异常,我使用下面的代码

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

标签:python-3-3,python,jira,python-jira
来源: https://codeday.me/bug/20190723/1508822.html