atlassian-python-api icon indicating copy to clipboard operation
atlassian-python-api copied to clipboard

issue with raise_for_status()

Open slmn-sh opened this issue 3 years ago • 9 comments

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

slmn-sh avatar Jan 07 '22 12:01 slmn-sh

Hi @salmannotkhan , noted. let me wrap KeyError: 'errorMessages'

gonchik avatar Jan 07 '22 13:01 gonchik

Thanks for the quick response :)

slmn-sh avatar Jan 07 '22 14:01 slmn-sh

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

slmn-sh avatar Jan 07 '22 17:01 slmn-sh

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.

flichtenheld avatar Jan 19 '22 15:01 flichtenheld

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'

wide0s avatar May 24 '22 17:05 wide0s

Let me merge and polish the next PR #972

gonchik avatar May 24 '22 19:05 gonchik

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 avatar May 27 '22 12:05 boidolr

@boidolr could you post the json from variable -> j = response.json() please? Maybe better fully wrap an error items.

gonchik avatar Jun 05 '22 21:06 gonchik

@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", "")

boidolr avatar Jun 06 '22 08:06 boidolr

@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())]

cguldner avatar Sep 26 '22 15:09 cguldner

@gonchik Have you had some time to look at this? Is there any way we can help?

juanmacoo avatar Jan 04 '23 08:01 juanmacoo

fc620ae094c162f36d93ceaa0998ebbc1ca9bf20

gonchik avatar Jan 04 '23 15:01 gonchik

So let me release it

gonchik avatar Jan 04 '23 15:01 gonchik

@flichtenheld @juanmacoo thank you for your effort :)

gonchik avatar Jan 04 '23 16:01 gonchik

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.

itsidorkin avatar Apr 18 '23 08:04 itsidorkin

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.

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.

nyphoon avatar May 15 '23 07:05 nyphoon