Coverage for src/xrayradar/integrations/graphene.py: 100%
28 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-14 15:56 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-14 15:56 +0000
1"""
2Graphene (GraphQL) integration for error tracking.
4GraphQL frameworks often catch resolver exceptions and return them as part of the
5GraphQL response, so Django's normal exception hooks may not see them.
7This middleware captures resolver exceptions and forwards them to xrayradar.
8"""
10from __future__ import annotations
12from typing import Any, Optional
14from ..client import ErrorTracker, get_client
17class GrapheneIntegration:
18 """Graphene middleware that captures resolver exceptions."""
20 def __init__(self, client: Optional[ErrorTracker] = None):
21 self.client = client
23 def resolve(self, next_, root, info, **kwargs):
24 try:
25 return next_(root, info, **kwargs)
26 except Exception as exc:
27 client = self.client or get_client()
28 if client is None:
29 client = ErrorTracker()
31 request = getattr(info, "context", None)
32 request_data: Optional[dict[str, Any]] = None
33 if request is not None and hasattr(request, "build_absolute_uri"):
34 headers = dict(getattr(request, "headers", {}) or {})
35 request_data = {
36 "url": request.build_absolute_uri(),
37 "method": getattr(request, "method", None),
38 "headers": headers,
39 "query_string": request.META.get("QUERY_STRING", "") if hasattr(request, "META") else "",
40 "remote_addr": request.META.get("REMOTE_ADDR") if hasattr(request, "META") else None,
41 }
43 operation_type = None
44 try:
45 operation = getattr(info, "operation", None)
46 if operation is not None and getattr(operation, "operation", None) is not None:
47 operation_type = str(operation.operation)
48 except Exception: # pragma: no cover
49 operation_type = None
51 tags: dict[str, str] = {"framework": "graphene"}
52 if operation_type:
53 tags["operation"] = operation_type
55 client.capture_exception(
56 exc, request=request_data or {}, tags=tags)
57 raise