Skip to content

Conversation

@rahul2393
Copy link
Contributor

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:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

@rahul2393 rahul2393 requested review from a team as code owners October 10, 2025 05:45
@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label Oct 10, 2025
@product-auto-label product-auto-label bot added the api: spanner Issues related to the googleapis/python-spanner API. label Oct 10, 2025
@rahul2393 rahul2393 force-pushed the enhance-error-message branch from 1632f7b to 07d0f5d Compare January 7, 2026 07:51
@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Jan 7, 2026
@rahul2393 rahul2393 force-pushed the enhance-error-message branch from 07d0f5d to 6402772 Compare January 7, 2026 08:58
@rahul2393
Copy link
Contributor Author

/gemini review

gemini-code-assist[bot]

This comment was marked as outdated.

@rahul2393 rahul2393 force-pushed the enhance-error-message branch 2 times, most recently from b8ea557 to ddb8563 Compare January 7, 2026 14:56
@product-auto-label product-auto-label bot added size: xl Pull request size is extra large. and removed size: l Pull request size is large. labels Jan 7, 2026
@rahul2393 rahul2393 force-pushed the enhance-error-message branch 4 times, most recently from 7fe169b to d98cebc Compare January 12, 2026 09:53
@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: xl Pull request size is extra large. labels Jan 12, 2026
@rahul2393 rahul2393 force-pushed the enhance-error-message branch from d98cebc to defd6aa Compare January 12, 2026 10:00
@rahul2393
Copy link
Contributor Author

/gemini review

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

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.

Comment on lines +40 to +41
if hasattr(error, "message") and error.message:
error.message = f"{error.message}, request_id = {request_id}"

Choose a reason for hiding this comment

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

critical

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.

Suggested change
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:]

Copy link
Contributor Author

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).

@rahul2393 rahul2393 requested a review from olavloite January 12, 2026 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the googleapis/python-spanner API. size: l Pull request size is large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants