Skip to content

Commit

Permalink
fix: Invalid context on GraphQL request errors (#4567)
Browse files Browse the repository at this point in the history
Fixes SentryNetworkTracker to add an object for GraphQL context instead of a string.

Fixes GH-4560
  • Loading branch information
jmccance authored Dec 3, 2024
1 parent dbb4b19 commit f05aa59
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Fix GraphQL context for HTTP client error tracking (#4567)

### Improvements

- Track adoption of `enablePersistingTracesWhenCrashing` (#4587)
Expand Down
6 changes: 4 additions & 2 deletions Sources/Sentry/SentryNetworkTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,10 @@ - (void)captureFailedRequests:(NSURLSessionTask *)sessionTask
context[@"response"] = response;

if (self.isGraphQLOperationTrackingEnabled) {
context[@"graphql_operation_name"] =
[URLSessionTaskHelper getGraphQLOperationNameFrom:sessionTask];
NSString *operationName = [URLSessionTaskHelper getGraphQLOperationNameFrom:sessionTask];
if (operationName != nil) {
context[@"graphql"] = @{ @"operation_name" : operationName };
}
}

event.context = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,47 @@ class SentryNetworkTrackerTests: XCTestCase {
XCTAssertEqual(sentryRequest.fragment, "myFragment")
XCTAssertEqual(sentryRequest.queryString, "query=myQuery")
}

func testCaptureHTTPClientErrorRequest_graphQLEnabled() throws {
let sut = fixture.getSut()

let task = createDataTask {
var request = $0

request.httpMethod = "POST"
request.httpBody = """
{
"operationName": "someOperationName",
"variables": { "a": 1 },
"query": "query someOperationName { someField }"
}
""".data(using: .utf8)
request.allHTTPHeaderFields = ["content-type": "application/json"]

return request
}
task.setResponse(createResponse(code: 500))

sut.urlSessionTask(task, setState: .completed)

let envelope = try XCTUnwrap(
fixture.hub.capturedEventsWithScopes.first,
"Expected to capture 1 event"
)

let graphQLContext = try XCTUnwrap(
envelope.event.context?["graphql"],
"Expected 'graphql' object in context"
)

XCTAssertEqual(graphQLContext.count, 1)
let operationName = try XCTUnwrap(
graphQLContext["operation_name"] as? String,
"Expected graphql.operation_name to be a String"
)

XCTAssertEqual(operationName, "someOperationName")
}

func testCaptureHTTPClientErrorRequest_noSecurityInfo() {
let sut = fixture.getSut()
Expand Down

0 comments on commit f05aa59

Please sign in to comment.