python - 捕获 jira-python 异常

标签 python jira python-3.3 python-jira

我正在尝试处理 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 - 捕获 jira-python 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18368629/

相关文章:

django - 升级到 Python 3.3 和设置 Django 的问题

python - Python中双重递归的结果

c++ - 在游戏机上加载的最佳数据文件格式

java - 如何以编程方式将值保存到 JIRA 中的自定义字段中?

python - 如何去除所有整数和空格的字符串

python-3.x - 我的 scipy.misc 模块似乎缺少 imsave

python - 在 python 中搜索字典列表

python - numpy 数组 <=> python DB-API 适配器?

python - 如何使用 jira-python 设置 fixVersions 字段

java - 错误领域=插件>com.sun.jersey.contribs :maven-wadl-plugin:1. 17