Skip to content

Comments

Update api.py#191

Merged
jessikad-google merged 1 commit intomasterfrom
jessikad-google-patch-1
Apr 9, 2025
Merged

Update api.py#191
jessikad-google merged 1 commit intomasterfrom
jessikad-google-patch-1

Conversation

@jessikad-google
Copy link
Contributor

Adding try catch so if error is coming from issue with decoding here we will know in the logs

Adding try catch so if error is coming from issue with decoding here we will know in the logs
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @jessikad-google, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request adds a try-except block to handle potential UnicodeDecodeError exceptions that may occur when decoding the content of a web page. Specifically, the change is in github_nonpublic_api/api.py within the download_dormant_users_report function. The original code directly decoded the page content using UTF-8, which could lead to errors if the content was not actually UTF-8 encoded. The updated code now attempts to decode the content within a try block, and if a UnicodeDecodeError occurs, it raises a ValueError with a descriptive message, which will help with debugging.

Highlights

  • Error Handling: Implements a try-except block to catch UnicodeDecodeError when decoding page content.
  • Exception Handling: Raises a ValueError with a descriptive message if UTF-8 decoding fails.

Changelog

  • github_nonpublic_api/api.py
    • Added a try-except block to handle potential UnicodeDecodeError during page content decoding.
    • If a UnicodeDecodeError occurs, a ValueError is raised with a message indicating the decoding failure.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A decode error's sting,
A try-catch block, a safe thing,
Logs will now show,
What we need to know,
When encodings take wing.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request adds a try-except block to handle potential UnicodeDecodeError exceptions when decoding the page content. This is a good addition to improve the robustness of the code.

Merge Readiness

The code change is a valuable addition to improve error handling. I recommend merging this pull request after addressing the review comments.

Comment on lines +263 to +266
try:
page_content = page.content.decode("utf-8")
except UnicodeDecodeError:
raise ValueError("Unable to decode page content as UTF-8.")

Choose a reason for hiding this comment

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

medium

This try-except block is a good addition for handling potential decoding errors. Consider adding a more specific log message within the except block to provide more context about the error. This will help in debugging if this error occurs in production.

        try:
            page_content = page.content.decode("utf-8")
        except UnicodeDecodeError as e:
            logging.error(f"UnicodeDecodeError: {e}") # Add logging here
            raise ValueError("Unable to decode page content as UTF-8.")

Copy link
Contributor

Choose a reason for hiding this comment

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

Gemini is sorta wrong here IMHO. I think it's perfectly OK to bubble up the UnicodeDecodeErrror as it without having to wrap it to ValueError. Mainly because there's no value to the caller to get one of those thrown vs. the other.

another nit (optional) is that the right text encoding scheme is returned from the response headers in the HTTP request. Is it worth fixing? Probably not, cuz I'm concerned it's a lot of work.

Choose a reason for hiding this comment

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

Bill, thanks for pointing that out. I agree that re-raising UnicodeDecodeError directly is a reasonable approach, especially if the caller doesn't need to distinguish between decoding errors and other ValueError types. Wrapping it adds a layer of abstraction that might not be necessary in this case.

Regarding the encoding, you're right that the response headers should ideally be checked for the correct encoding. Given the potential complexity and the fact that UTF-8 is a widely used and generally safe default, skipping that for now seems like a pragmatic decision.

@jessikad-google jessikad-google enabled auto-merge (squash) April 9, 2025 18:47
@jessikad-google jessikad-google merged commit 6181aa7 into master Apr 9, 2025
5 checks passed
@jessikad-google jessikad-google deleted the jessikad-google-patch-1 branch April 9, 2025 18:48
Comment on lines +263 to +266
try:
page_content = page.content.decode("utf-8")
except UnicodeDecodeError:
raise ValueError("Unable to decode page content as UTF-8.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Gemini is sorta wrong here IMHO. I think it's perfectly OK to bubble up the UnicodeDecodeErrror as it without having to wrap it to ValueError. Mainly because there's no value to the caller to get one of those thrown vs. the other.

another nit (optional) is that the right text encoding scheme is returned from the response headers in the HTTP request. Is it worth fixing? Probably not, cuz I'm concerned it's a lot of work.

jessikad-google added a commit that referenced this pull request Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants