gpt4 book ai didi

python - 捕获 jira-python 异常

转载 作者:行者123 更新时间:2023-12-02 06:54:33 27 4
gpt4 key购买 nike

我正在尝试处理 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com