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

fix a potential json parsing error #485

Closed
wants to merge 1 commit into from

Conversation

ziliangpeng
Copy link

As discussed here, there's a chance (which I have experienced) that the response of the oauth request is not in json format, resulting in json parsing error.

This PR fixes it by capturing the exception and convert to a RefreshError.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Apr 7, 2020
@ziliangpeng
Copy link
Author

@georgysavva @busunkim96

@georgysavva
Copy link
Contributor

Why not to just move json.loads() after the http_client.OK check. As it was before my change?

@busunkim96 busunkim96 self-requested a review April 7, 2020 16:37
@ziliangpeng
Copy link
Author

@georgysavva The decoded json needs to be used in the error handling as well as being a return value.

I believe keeping it here is the most appropriate place to decode it. Where else exactly do you see is a better place?

@alvyjudy
Copy link
Contributor

alvyjudy commented May 4, 2020

Hi, please correct me if I'm wrong but it appears that this problem (response not in json format) kind of occur by chance? (I get this impression after reading the discussion you mentioned)

So when it arises wouldn't it be a better idea to re-fetch, rather than giving the error back to the client? I would imagine the error message to be quite obscure.

Also, if it is indeed the API that wouldn't return a json response, would it be better to place your proposed functionality inside _handle_error_message? I feel that would make the code look cleaner.

response_data = json.loads(response_body)
try:
response_data = json.loads(response_body)
except (KeyError, ValueError):
Copy link
Contributor

@mik-laj mik-laj Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
except (KeyError, ValueError):
except json.DecodeError:

WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a Python3-only change, i think.

Copy link
Contributor

@tseaver tseaver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than doing remapping of the error twice (once above and once below the check of response.status, it would be better to move the parsing inside the block for an OK status. E.g.:

        if response.status == http_client.OK:
            return json.loads(response_body)

        if response.content_type != "application/json":
            error_details = response_body
            raise exceptions.RefreshError(error_details, response_body)

        response_data = json.decode(response_body)
        error_desc = response_data.get("error_description") or ""
        error_code = response_data.get("error") or ""
        if (
            any(e == "internal_failure" for e in (error_code, error_desc))
            and retry < 1
        ):
            retry += 1
            continue
        _handle_error_response(response_body)

@busunkim96 busunkim96 closed this Jul 31, 2020
@busunkim96 busunkim96 reopened this Jul 31, 2020
@parthea
Copy link
Contributor

parthea commented Aug 14, 2021

Hi @ziliangpeng , I'm going to close this PR due to inactivity but please feel free to re-open it.

@parthea parthea closed this Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants