Skip to content

Commit

Permalink
COI-268 - Fixed an issue with an unexpected error message format (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkah authored Dec 3, 2024
1 parent d2f88e3 commit 5dfa7d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
12 changes: 11 additions & 1 deletion Sources/GQLFetcher/Errors/GraphQLError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ public struct GraphQLError: CustomStringConvertible {
public let locations: [Location]?

init(data: GraphQLJSON) {
self.message = (data["message"] as? String) ?? "Uknown error"
if let message = data["message"] as? String {
self.message = message
} else {
if let messageData = data["message"], let jsonData = try? JSONSerialization.data(withJSONObject: messageData as Any, options: []),
let jsonString = String(data: jsonData, encoding: .utf8) {
self.message = jsonString
} else {
self.message = "Unknown error"
}
}

self.fields = data["fields"] as? [String]

if let locations = data["locations"] as? [GraphQLJSON] {
Expand Down
20 changes: 18 additions & 2 deletions Tests/GQLFetcherTests/GraphQLErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class GraphQLErrorTests: XCTestCase {

func testUnnownError() {
let error = GraphQLError(data: [:])
XCTAssertEqual(error.message, "Uknown error")
XCTAssertEqual(error.message, "Unknown error")
XCTAssertNil(error.fields)
XCTAssertNil(error.locations)
}
Expand All @@ -59,7 +59,23 @@ class GraphQLErrorTests: XCTestCase {
XCTAssertEqual(error.locations?.first?.line, 2)
XCTAssertEqual(error.locations?.first?.column, 56)
}


func testIncompatibkeErrorMessage() {
let _error : GraphQLJSON = [
"message": ["Errors": ["invalidargument": -333]],
"line": 2,
"column": 56,
"fields": [
10
]
]
let error = GraphQLError(data: _error)
XCTAssertEqual(error.message, "{\"Errors\":{\"invalidargument\":-333}}")
XCTAssertNil(error.fields)
XCTAssertEqual(error.locations?.first?.line, 2)
XCTAssertEqual(error.locations?.first?.column, 56)
}

static var allTests = [
("testInit", testInit),
("testUnnownError", testUnnownError),
Expand Down

0 comments on commit 5dfa7d7

Please sign in to comment.