Conversation
Adding try catch so if error is coming from issue with decoding here we will know in the logs
There was a problem hiding this comment.
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
UnicodeDecodeErrorwhen decoding page content. - Exception Handling: Raises a
ValueErrorwith a descriptive message if UTF-8 decoding fails.
Changelog
- github_nonpublic_api/api.py
- Added a try-except block to handle potential
UnicodeDecodeErrorduring page content decoding. - If a
UnicodeDecodeErroroccurs, aValueErroris raised with a message indicating the decoding failure.
- Added a try-except block to handle potential
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
-
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. ↩
There was a problem hiding this comment.
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.
| try: | ||
| page_content = page.content.decode("utf-8") | ||
| except UnicodeDecodeError: | ||
| raise ValueError("Unable to decode page content as UTF-8.") |
There was a problem hiding this comment.
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.")There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| try: | ||
| page_content = page.content.decode("utf-8") | ||
| except UnicodeDecodeError: | ||
| raise ValueError("Unable to decode page content as UTF-8.") |
There was a problem hiding this comment.
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.
This reverts commit 6181aa7.
Adding try catch so if error is coming from issue with decoding here we will know in the logs