Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ddtrace/contrib/internal/graphql/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,13 @@ def _set_span_errors(errors: List[GraphQLError], span: Span) -> None:
error_msgs = "\n".join([str(error) for error in errors])
span.set_tag_str(ERROR_MSG, error_msgs)
for error in errors:
locations = [f"{loc.formatted['line']}:{loc.formatted['column']}" for loc in error.locations]
attributes = {
"message": error.message,
"type": span.get_tag("error.type"),
"locations": locations,
}
if error.locations:
locations = [f"{loc.formatted['line']}:{loc.formatted['column']}" for loc in error.locations]
attributes["locations"] = locations

if error.__traceback__:
exc_type, exc_val, exc_tb = type(error), error, error.__traceback__
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
GraphQL: Fixes an issue in GraphQL patching that threw an error when locations (an `Optional` field) is `None`. This checks for locations before setting attributes.
12 changes: 12 additions & 0 deletions tests/contrib/graphql/test_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ def test_graphql_execute_sync_with_middlware_manager(
assert res2.data == {"hello": "friend"}


@snapshot(ignores=["meta.error.stack", "meta.events"])
def test_regression_graphql_error_locations_none():
"""
Ensure GraphQLError with `locations=None` does not cause patching issues.
"""
error = graphql.GraphQLError(message="Test error with no locations")
error.locations = None
result = graphql.ExecutionResult(data=None, errors=[error])

assert result.errors[0].locations is None


@pytest.mark.snapshot
@pytest.mark.skipif(graphql_version < (3, 0), reason="graphql.graphql_sync is NOT supported in v2.0")
@pytest.mark.parametrize(
Expand Down
Loading