-
Notifications
You must be signed in to change notification settings - Fork 159
RUM-12441 Graphql errors #2552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
RUM-12441 Graphql errors #2552
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6bb7f76
RUM-12441 Track errors in GraphQL requests
barboraplasovska dc60d26
RUM-12441 Tests for GraphQL error tracking
barboraplasovska ecf3664
RUM-12441 Update the changelog
barboraplasovska 2b6c969
RUM-12441 Use ExpectedGraphQLHeaders in tests
barboraplasovska e4bcbcf
RUM-12441 Apply Maxime's suggestions
barboraplasovska 3ef2031
RUM-12441 Don't extract errors key in ResourcesHandler
barboraplasovska 264acfe
RUM-12441 Fix merge conflicts
barboraplasovska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2019-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| import Foundation | ||
|
|
||
| // MARK: - GraphQL Response Models | ||
|
|
||
| /// Lightweight decoder to check if a GraphQL response contains errors. | ||
| /// Only checks for the presence of the "errors" key without decoding the entire array. | ||
| internal struct GraphQLResponseHasErrors: Decodable { | ||
| let hasErrors: Bool | ||
| private enum CodingKeys: String, CodingKey { case errors } | ||
|
|
||
| init(from decoder: Decoder) throws { | ||
| let container = try decoder.container(keyedBy: CodingKeys.self) | ||
| hasErrors = container.contains(.errors) | ||
| } | ||
| } | ||
|
|
||
| /// Full decoder for GraphQL response with errors array. | ||
| /// Used when we need to extract the actual error details. | ||
| internal struct GraphQLResponse: Decodable { | ||
| let errors: [GraphQLResponseError]? | ||
| } | ||
|
|
||
| /// Represents a GraphQL error in the response. | ||
| /// | ||
| /// Note: Some GraphQL implementations may include `code` at the error level (legacy pattern) | ||
| /// instead of within `extensions.code`. Both locations are supported for compatibility. | ||
| /// Reference: https://spec.graphql.org/September2025/#note-5c13b | ||
| internal struct GraphQLResponseError: Decodable { | ||
| let message: String | ||
| let locations: [GraphQLResponseErrorLocation]? | ||
| let path: [GraphQLResponsePathElement]? | ||
| let extensions: Extensions? | ||
|
|
||
| /// Error code extracted from either `extensions.code` (preferred) or top-level `code` (legacy). | ||
| var code: String? { | ||
| return extensions?.code ?? legacyCode | ||
| } | ||
|
|
||
| /// Legacy code field that some implementations put at the error level instead of in extensions. | ||
| private let legacyCode: String? | ||
|
|
||
| private enum CodingKeys: String, CodingKey { | ||
| case message | ||
| case locations | ||
| case path | ||
| case extensions | ||
| case legacyCode = "code" | ||
| } | ||
|
|
||
| /// GraphQL error extensions. Only the `code` field is extracted as it's the most commonly used. | ||
| /// The GraphQL spec allows any additional fields in extensions, but we focus on error codes. | ||
| struct Extensions: Decodable { | ||
| let code: String? | ||
| } | ||
| } | ||
|
|
||
| /// Represents a location in a GraphQL query where an error occurred. | ||
| internal struct GraphQLResponseErrorLocation: Decodable { | ||
| let line: Int | ||
| let column: Int | ||
| } | ||
|
|
||
| /// Represents an element in the path to a field that caused an error. | ||
| internal enum GraphQLResponsePathElement: Decodable { | ||
| case string(String) | ||
| case int(Int) | ||
| init(from decoder: Decoder) throws { | ||
| let container = try decoder.singleValueContainer() | ||
| if let intValue = try? container.decode(Int.self) { | ||
| self = .int(intValue) | ||
| } else if let stringValue = try? container.decode(String.self) { | ||
| self = .string(stringValue) | ||
| } else { | ||
| throw DecodingError.dataCorruptedError( | ||
| in: container, | ||
| debugDescription: "Path element must be string or int" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
smart!