Skip to content

Commit df33807

Browse files
authored
refactor(gapic): simplify error attribute extraction logic
Simplify domain, reason, and metadata checks in _extract_error_attributes by relying on standard truthiness checks.
1 parent 099e8df commit df33807

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

packages/google-api-core/google/api_core/gapic_v1/method.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,13 @@ def _extract_error_attributes(exc: Optional[Exception]) -> dict[str, Any]:
211211
# 4. Unified attribute extraction: extract domain, reason, and metadata from ErrorInfo or exception attributes
212212
source = error_info or target_exc
213213
domain = getattr(source, "domain", None)
214-
if domain and isinstance(domain, str):
214+
if domain:
215215
attrs["gcp.errors.domain"] = domain
216216
reason = getattr(source, "reason", None)
217-
if reason and isinstance(reason, str):
217+
if reason:
218218
attrs["error.type"] = reason
219219
metadata = getattr(source, "metadata", None)
220-
if metadata and hasattr(metadata, "items"):
220+
if metadata:
221221
for k, v in metadata.items():
222222
attrs[f"gcp.errors.metadata.{k}"] = str(v)
223223

packages/google-api-core/tests/unit/gapic/test_method.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -818,10 +818,10 @@ def test_extract_error_attributes_variations():
818818
):
819819
assert _extract_error_attributes(exc_with_resp) == {}
820820

821-
# 4. error_info with non-string domain, non-string reason, non-mapping metadata
822-
error_info_invalid = types.SimpleNamespace(domain=123, reason=None, metadata=None)
823-
exc_invalid = types.SimpleNamespace(error_info=error_info_invalid)
824-
assert _extract_error_attributes(exc_invalid) == {}
821+
# 4. error_info with empty domain, empty reason, empty metadata
822+
error_info_empty = types.SimpleNamespace(domain="", reason="", metadata=None)
823+
exc_empty = types.SimpleNamespace(error_info=error_info_empty)
824+
assert _extract_error_attributes(exc_empty) == {}
825825

826826
# 5. else fallback where target_exc directly has domain, reason, and metadata
827827
exc_fallback = types.SimpleNamespace(
@@ -835,13 +835,13 @@ def test_extract_error_attributes_variations():
835835
"gcp.errors.metadata.f_key": "42",
836836
}
837837

838-
# 6. else fallback with invalid types (e.g. domain="", reason=123, metadata="not a dict")
839-
exc_fallback_invalid = types.SimpleNamespace(
838+
# 6. else fallback with empty attributes (e.g. domain="", reason="", metadata={})
839+
exc_fallback_empty = types.SimpleNamespace(
840840
domain="",
841-
reason=123,
842-
metadata="string_without_items",
841+
reason="",
842+
metadata={},
843843
)
844-
assert _extract_error_attributes(exc_fallback_invalid) == {}
844+
assert _extract_error_attributes(exc_fallback_empty) == {}
845845

846846

847847
def test_wrap_method_otel_tracing_partial_span_capabilities(mock_otel):

0 commit comments

Comments
 (0)