atlassian-python-api
atlassian-python-api copied to clipboard
issue with raise_for_status()
Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/atlassian/rest_client.py", line 395, in raise_for_status error_msg = "\n".join(j["errorMessages"] + [k + ": " + v for k, v in j["errors"].items()]) KeyError: 'errorMessages'
this happens when I try to add labels to any repo using bitbucket.set_repo_label() function
I think this commit is the reason of this issue because I'm regular user of this library and never faced anything like this https://github.com/atlassian-api/atlassian-python-api/commit/50b5c71c05fa31f14dc9dcf1e3b89b88ce56df58
Hi @salmannotkhan ,
noted. let me wrap KeyError: 'errorMessages'
Thanks for the quick response :)
This is the response from the server. {'errors': [{'context': None, 'message': "Label name '0D930811-B3F7-4875-97BD-7DF252F907CA' cannot contain capital letters.", 'exceptionName': 'com.atlassian.bitbucket.label.InvalidLabelException'}]} You can update script accordingly
Note: I provided a fix for Bitbucket Cloud in PR #925 but it looks like Bitbucket Server will need a separate solution since the error format differs.
Is there any update for BitBucket Server? I see it with Bitbucket v8.0.0:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/atlassian/rest_client.py", line 395, in raise_for_status
error_msg = "\n".join(j["errorMessages"] + [k + ": " + v for k, v in j["errors"].items()])
KeyError: 'errorMessages'
Let me merge and polish the next PR #972
Unfortunately there still is an error with 3.25.0:
atlassian/rest_client.py", line 396, in raise_for_status
j.get("errorMessages", list()) + [k + ": " + v for k, v in j.get("errors", dict()).items()]
AttributeError: 'list' object has no attribute 'items'
@boidolr could you post the json from variable -> j = response.json()
please?
Maybe better fully wrap an error items.
@gonchik this happens if I either get or provoke an error, the object looks like this:
{'errors': [{'context': None, 'message': '....', 'exceptionName': 'com.atlassian.bitbucket.pull.InvalidPullRequestTargetException'}]}
Minimal example - the server is running v7.21.0 LTS:
from atlassian import Bitbucket
bb = Bitbucket(url=url, username=user, password=token)
bb.open_pull_request("PROJ", "repo", "PROJ", "repo", "main", "main", "title", "")
@gonchik The issue is resolved if you replace this line with:
j.get("errorMessages", list()) + [k.get("message", "") for k in j.get("errors", dict())]
@gonchik Have you had some time to look at this? Is there any way we can help?
fc620ae094c162f36d93ceaa0998ebbc1ca9bf20
So let me release it
@flichtenheld @juanmacoo thank you for your effort :)
This fix is breaking the error message handling in the Jira module. For example, when trying to create a ticket without specifying the 'project' field, prior to version 3.32.2, the error message would correctly indicate that
Traceback (most recent call last):
File "...\scratches\scratch.py", line 345, in <module>
jira.issue_create(
File "...\Lib\site-packages\atlassian\jira.py", line 1344, in issue_create
return self.post(url, data={"fields": fields})
File "...\Lib\site-packages\atlassian\rest_client.py", line 333, in post
response = self.request(
File "...\Lib\site-packages\atlassian\rest_client.py", line 257, in request
self.raise_for_status(response)
File "...\Lib\site-packages\atlassian\rest_client.py", line 442, in raise_for_status
raise HTTPError(error_msg, response=response)
requests.exceptions.HTTPError: project: project is required
However, starting with version 3.32.2, the error message now shows
Traceback (most recent call last):
File "...\Lib\site-packages\atlassian\rest_client.py", line 437, in raise_for_status
j.get("errorMessages", list()) + [k.get("message", "") for k in j.get("errors", dict())]
File "...\Lib\site-packages\atlassian\rest_client.py", line 437, in <listcomp>
j.get("errorMessages", list()) + [k.get("message", "") for k in j.get("errors", dict())]
AttributeError: 'str' object has no attribute 'get'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...\scratches\scratch.py", line 345, in <module>
jira.issue_create(
File "...\Lib\site-packages\atlassian\jira.py", line 1344, in issue_create
return self.post(url, data={"fields": fields})
File "...\Lib\site-packages\atlassian\rest_client.py", line 333, in post
response = self.request(
File "...\Lib\site-packages\atlassian\rest_client.py", line 257, in request
self.raise_for_status(response)
File "...\Lib\site-packages\atlassian\rest_client.py", line 441, in raise_for_status
response.raise_for_status()
File "...\Lib\site-packages\requests\models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: for url: https://mysite.com/rest/api/2/issue
If we print the value of j
from rest_client.py, we can see that
{'errorMessages': [], 'errors': {'project': 'project is required'}}.
There is no 'message' key in this dictionary, and k
is just the string 'project', which cannot be used with the get
method.
This fix is breaking the error message handling in the Jira module. For example, when trying to create a ticket without specifying the 'project' field, prior to version 3.32.2, the error message would correctly indicate that
Traceback (most recent call last): File "...\scratches\scratch.py", line 345, in <module> jira.issue_create( File "...\Lib\site-packages\atlassian\jira.py", line 1344, in issue_create return self.post(url, data={"fields": fields}) File "...\Lib\site-packages\atlassian\rest_client.py", line 333, in post response = self.request( File "...\Lib\site-packages\atlassian\rest_client.py", line 257, in request self.raise_for_status(response) File "...\Lib\site-packages\atlassian\rest_client.py", line 442, in raise_for_status raise HTTPError(error_msg, response=response) requests.exceptions.HTTPError: project: project is required
However, starting with version 3.32.2, the error message now shows
Traceback (most recent call last): File "...\Lib\site-packages\atlassian\rest_client.py", line 437, in raise_for_status j.get("errorMessages", list()) + [k.get("message", "") for k in j.get("errors", dict())] File "...\Lib\site-packages\atlassian\rest_client.py", line 437, in <listcomp> j.get("errorMessages", list()) + [k.get("message", "") for k in j.get("errors", dict())] AttributeError: 'str' object has no attribute 'get' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "...\scratches\scratch.py", line 345, in <module> jira.issue_create( File "...\Lib\site-packages\atlassian\jira.py", line 1344, in issue_create return self.post(url, data={"fields": fields}) File "...\Lib\site-packages\atlassian\rest_client.py", line 333, in post response = self.request( File "...\Lib\site-packages\atlassian\rest_client.py", line 257, in request self.raise_for_status(response) File "...\Lib\site-packages\atlassian\rest_client.py", line 441, in raise_for_status response.raise_for_status() File "...\Lib\site-packages\requests\models.py", line 1021, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: for url: https://mysite.com/rest/api/2/issue
If we print the value of
j
from rest_client.py, we can see that
{'errorMessages': [], 'errors': {'project': 'project is required'}}.
There is no 'message' key in this dictionary, and
k
is just the string 'project', which cannot be used with theget
method.
I'm also having this issue. It's a confusing information about AttributeError
when touble-shooting.
There must be something wrong because k
is string key from JSON. You cannot call get
method from a string.