Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while handling response code 422 #139

Open
dhruvmanila opened this issue Dec 10, 2020 · 3 comments
Open

Error while handling response code 422 #139

dhruvmanila opened this issue Dec 10, 2020 · 3 comments
Labels
area-REST GitHub REST API bug

Comments

@dhruvmanila
Copy link
Contributor

It seems that gidgethub is not able to handle a part of the 422 response. This happened when the app tried to make a review comment on a line that was not part of the pull request diff. I didn't know that was not possible but this mistake revealed this bug in the code.

{
  "message": "Unprocessable Entity",
  "errors": [
    "Line must be part of the diff"
  ],
  "documentation_url": "https://docs.github.com/rest/reference/pulls#create-a-review-for-a-pull-request", 
}

The code that triggered the error, taken from gidgethub.sansio.decipher_response:

                exc_type = InvalidField
                if errors:
                    if any(
       ------>          e["code"]
                        in ["missing", "missing_field", "invalid", "already_exists"]
                        for e in errors
                    ):
                        error_context = ", ".join(repr(e.get("field")) for e in errors)
                        message = f"{message} for {error_context}"

Logged traceback:

2020-12-10T14:57:43.757626+00:00 app[web.1]:     await gh.post(
2020-12-10T14:57:43.757626+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gidgethub/abc.py", line 175, in post
2020-12-10T14:57:43.757626+00:00 app[web.1]:     data, _ = await self._make_request(
2020-12-10T14:57:43.757626+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gidgethub/abc.py", line 114, in _make_request
2020-12-10T14:57:43.757627+00:00 app[web.1]:     data, self.rate_limit, more = sansio.decipher_response(*response)
2020-12-10T14:57:43.757627+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gidgethub/sansio.py", line 340, in decipher_response
2020-12-10T14:57:43.757628+00:00 app[web.1]:     if any(
2020-12-10T14:57:43.757628+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gidgethub/sansio.py", line 341, in <genexpr>
2020-12-10T14:57:43.757628+00:00 app[web.1]:     e["code"]
2020-12-10T14:57:43.757628+00:00 app[web.1]: TypeError: string indices must be integers
@brettcannon
Copy link
Collaborator

Ugh, the one thing about error cases that seemed to be consistent isn't consistent.

@brettcannon brettcannon added area-REST GitHub REST API bug labels Dec 10, 2020
@dhruvmanila
Copy link
Contributor Author

A quick fix would be to check whether the key code exists in the errors object, otherwise accumulate whatever is in the errors object and combine it with the message:

error_context = ", ".join(repr(e) for e in errors)
message = f"{message}: {error_context}"

@Mariatta
Copy link
Member

Mariatta commented Aug 4, 2021

Feels like it's a bug with with GitHub, since the behavior described is not documented at all.
https://docs.github.com/en/rest/overview/resources-in-the-rest-api#client-errors

Maybe worth reporting the problem with GitHub, so they can either fix the doc or the response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-REST GitHub REST API bug
Projects
None yet
Development

No branches or pull requests

3 participants