|
22 | 22 | import threading |
23 | 23 | import logging |
24 | 24 | import uuid |
| 25 | +from contextlib import contextmanager |
25 | 26 |
|
26 | 27 | from google.protobuf.struct_pb2 import ListValue |
27 | 28 | from google.protobuf.struct_pb2 import Value |
|
34 | 35 | from google.cloud.spanner_v1.types import ExecuteSqlRequest |
35 | 36 | from google.cloud.spanner_v1.types import TransactionOptions |
36 | 37 | from google.cloud.spanner_v1.data_types import JsonObject, Interval |
37 | | -from google.cloud.spanner_v1.request_id_header import with_request_id |
| 38 | +from google.cloud.spanner_v1.request_id_header import ( |
| 39 | + with_request_id, |
| 40 | + with_request_id_metadata_only, |
| 41 | +) |
38 | 42 | from google.cloud.spanner_v1.types import TypeCode |
| 43 | +from google.cloud.spanner_v1.exceptions import wrap_with_request_id |
39 | 44 |
|
40 | 45 | from google.rpc.error_details_pb2 import RetryInfo |
41 | 46 |
|
@@ -767,9 +772,65 @@ def reset(self): |
767 | 772 |
|
768 | 773 |
|
769 | 774 | def _metadata_with_request_id(*args, **kwargs): |
| 775 | + """Return metadata with request ID header. |
| 776 | +
|
| 777 | + This function returns only the metadata list (not a tuple), |
| 778 | + maintaining backward compatibility with existing code. |
| 779 | +
|
| 780 | + Args: |
| 781 | + *args: Arguments to pass to with_request_id |
| 782 | + **kwargs: Keyword arguments to pass to with_request_id |
| 783 | +
|
| 784 | + Returns: |
| 785 | + list: gRPC metadata with request ID header |
| 786 | + """ |
| 787 | + return with_request_id_metadata_only(*args, **kwargs) |
| 788 | + |
| 789 | + |
| 790 | +def _metadata_with_request_id_and_req_id(*args, **kwargs): |
| 791 | + """Return both metadata and request ID string. |
| 792 | +
|
| 793 | + This is used when we need to augment errors with the request ID. |
| 794 | +
|
| 795 | + Args: |
| 796 | + *args: Arguments to pass to with_request_id |
| 797 | + **kwargs: Keyword arguments to pass to with_request_id |
| 798 | +
|
| 799 | + Returns: |
| 800 | + tuple: (metadata, request_id) |
| 801 | + """ |
770 | 802 | return with_request_id(*args, **kwargs) |
771 | 803 |
|
772 | 804 |
|
| 805 | +def _augment_error_with_request_id(error, request_id=None): |
| 806 | + """Augment an error with request ID information. |
| 807 | +
|
| 808 | + Args: |
| 809 | + error: The error to augment (typically GoogleAPICallError) |
| 810 | + request_id (str): The request ID to include |
| 811 | +
|
| 812 | + Returns: |
| 813 | + The augmented error with request ID information |
| 814 | + """ |
| 815 | + return wrap_with_request_id(error, request_id) |
| 816 | + |
| 817 | + |
| 818 | +@contextmanager |
| 819 | +def _augment_errors_with_request_id(request_id): |
| 820 | + """Context manager to augment exceptions with request ID. |
| 821 | +
|
| 822 | + Args: |
| 823 | + request_id (str): The request ID to include in exceptions |
| 824 | +
|
| 825 | + Yields: |
| 826 | + None |
| 827 | + """ |
| 828 | + try: |
| 829 | + yield |
| 830 | + except Exception as exc: |
| 831 | + raise _augment_error_with_request_id(exc, request_id) |
| 832 | + |
| 833 | + |
773 | 834 | def _merge_Transaction_Options( |
774 | 835 | defaultTransactionOptions: TransactionOptions, |
775 | 836 | mergeTransactionOptions: TransactionOptions, |
|
0 commit comments