捕获jira-python异常

ary*_*ard 4 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)
Run Code Online (Sandbox Code Playgroud)

以下是引发异常的代码:

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)
Run Code Online (Sandbox Code Playgroud)

use*_*954 8

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

在任何情况下,这里的链接JIRA-Python代码为JIRAError类和这里的代码列表

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

from jira import JIRA, JIRAError
try:
   ...
except JIRAError as e:
   print e.status_code, e.text
Run Code Online (Sandbox Code Playgroud)


Jai*_*ago 5

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

from jira.exceptions import JIRAError
Run Code Online (Sandbox Code Playgroud)

你的代码中的某个地方对吗?

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


小智 2

我可能是错的,但看起来你jira.exceptions.JIRAError在加注时抓住了JIRAError- 这些是不同的类型。您需要jira.exceptions.从声明中删除“ ”部分except,或者jira.exceptions.JIRAError改为加注。