Skip to content

Commit

Permalink
add extensions field to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
maticzav committed May 2, 2023
1 parent 47d791a commit a973462
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Sources/GraphQL/Error.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Foundation

/// A GraphQLError describes an Error found during the parse, validate, or
/// execute phases of performing a GraphQL operation. In addition to a message
/// and stack trace, it also includes information about the locations in a
/// GraphQL document and/or execution result that correspond to the Error.
public struct GraphQLError: Codable, Equatable, Sendable {
/// A GraphQLError describes an Error found during the parse, validate, or execute phases of performing a GraphQL operation. In addition to a message and stack trace, it also includes information about the locations in a GraphQL document and/or execution result that correspond to the Error.
///
/// Its implementation follows the specification described at [GraphQLSpec](http://spec.graphql.org/October2021/#sec-Errors.Error-result-format).
public struct GraphQLError: Codable, Equatable {

/// A short, human-readable summary of the problem.
public let message: String
Expand Down Expand Up @@ -59,16 +58,21 @@ public struct GraphQLError: Codable, Equatable, Sendable {
}
}

/// A map of strings for implementors to add additional information to errors however they see fit, and there are no additional restrictions on its contents.
public let extensions: [String: AnyCodable]?

// MARK: - Initializer

public init(
message: String,
locations: [Location]? = nil,
path: [PathLink]? = nil
path: [PathLink]? = nil,
extensions: [String: AnyCodable]? = nil
) {
self.message = message
self.locations = locations
self.path = path
self.extensions = extensions
}
}

Expand Down
38 changes: 38 additions & 0 deletions Tests/SwiftGraphQLTests/Selection/SelectionDecodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,44 @@ final class SelectionDecodingTests: XCTestCase {
),
])
}

func testErrorResponse() throws {
let result: ExecutionResult = """
{
"errors": [
{
"message": "Bad Request Exception",
"extensions": {
"code": "BAD_USER_INPUT",
}
}
],
"data": null
}
""".execution()

let selection = Selection<String, String> {
switch $0.__state {
case let .decoding(data):
return try String(from: data)
case .selecting:
return "wrong"
}
}

let decoded = try selection.nullable.decode(raw: result.data)
XCTAssertEqual(decoded, nil)
XCTAssertEqual(result.errors, [
GraphQLError(
message: "Bad Request Exception",
locations: nil,
path: nil,
extensions: [
"code": AnyCodable("BAD_USER_INPUT")
]
)
])
}
}

extension String {
Expand Down

0 comments on commit a973462

Please sign in to comment.