-
Notifications
You must be signed in to change notification settings - Fork 99
feat: add requestID info in error exceptions #1415
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
base: main
Are you sure you want to change the base?
Conversation
1632f7b to
07d0f5d
Compare
07d0f5d to
6402772
Compare
|
/gemini review |
b8ea557 to
ddb8563
Compare
7fe169b to
d98cebc
Compare
d98cebc to
defd6aa
Compare
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a valuable feature for debugging by adding request ID information to exceptions. The implementation is comprehensive, using a new wrap_with_request_id utility and an _augment_errors_with_request_id context manager to consistently augment exceptions across numerous API call sites. The changes are well-tested, with updates to existing tests and new tests for the exception wrapping logic.
My review has identified a critical issue in how exception messages are modified, which could prevent the request ID from appearing in logs as intended. I have also pointed out a few areas where code duplication can be reduced to improve maintainability. Overall, this is a solid contribution that will improve the library's debuggability once the identified issues are addressed.
| if hasattr(error, "message") and error.message: | ||
| error.message = f"{error.message}, request_id = {request_id}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of modifying the error message by setting error.message is incorrect for GoogleAPICallError. It creates a new instance attribute that shadows the message property but does not change the underlying exception arguments. As a result, str(error) will not include the request_id, which would prevent it from appearing in logs.
To correctly update the exception message so that it's reflected when the exception is converted to a string, you should modify the error.args tuple.
| if hasattr(error, "message") and error.message: | |
| error.message = f"{error.message}, request_id = {request_id}" | |
| if error.message: | |
| new_message = f"{error.message}, request_id = {request_id}" | |
| error.args = (new_message,) + error.args[1:] | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested both approaches:
from google.api_core.exceptions import Aborted
Approach 1: Setting error.message (current implementation)
error1 = Aborted('Transaction aborted')
error1.message = f'{error1.message}, request_id = 123'
print(str(error1)) # Output: "409 Transaction aborted, request_id = 123" ✓
Approach 2: Setting error.args only (suggested change)
error2 = Aborted('Transaction aborted')
new_message = f'{error2.message}, request_id = 456'
error2.args = (new_message,) + error2.args[1:]
print(str(error2)) # Output: "409 Transaction aborted" ✗ (request_id missing!)
GoogleAPICallError.str uses the message property rather than args, so modifying args alone doesn't update the string representation. The current implementation correctly updates error.message which is reflected in str(error).
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #<issue_number_goes_here> 🦕