-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||||
| # Copyright 2026 Google LLC All rights reserved. | ||||||||||||||
| # | ||||||||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||
| # you may not use this file except in compliance with the License. | ||||||||||||||
| # You may obtain a copy of the License at | ||||||||||||||
| # | ||||||||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||
| # | ||||||||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||
| # See the License for the specific language governing permissions and | ||||||||||||||
| # limitations under the License. | ||||||||||||||
|
|
||||||||||||||
| """Cloud Spanner exception utilities with request ID support.""" | ||||||||||||||
|
|
||||||||||||||
| from google.api_core.exceptions import GoogleAPICallError | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def wrap_with_request_id(error, request_id=None): | ||||||||||||||
| """Add request ID information to a GoogleAPICallError. | ||||||||||||||
| This function adds request_id as an attribute to the exception, | ||||||||||||||
| preserving the original exception type for exception handling compatibility. | ||||||||||||||
| The request_id is also appended to the error message so it appears in logs. | ||||||||||||||
| Args: | ||||||||||||||
| error: The error to augment. If not a GoogleAPICallError, returns as-is | ||||||||||||||
| request_id (str): The request ID to include | ||||||||||||||
| Returns: | ||||||||||||||
| The original error with request_id attribute added and message updated | ||||||||||||||
| (if GoogleAPICallError and request_id is provided), otherwise returns | ||||||||||||||
| the original error unchanged. | ||||||||||||||
| """ | ||||||||||||||
| if isinstance(error, GoogleAPICallError) and request_id: | ||||||||||||||
| # Add request_id as an attribute for programmatic access | ||||||||||||||
| error.request_id = request_id | ||||||||||||||
| # Modify the message to include request_id so it appears in logs | ||||||||||||||
| if hasattr(error, "message") and error.message: | ||||||||||||||
| error.message = f"{error.message}, request_id = {request_id}" | ||||||||||||||
|
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of modifying the error message by setting To correctly update the exception message so that it's reflected when the exception is converted to a string, you should modify the
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') Approach 2: Setting error.args only (suggested change)error2 = Aborted('Transaction aborted') 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). |
||||||||||||||
| return error | ||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.