Skip to content

Commit

Permalink
Improved Error forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
terhechte committed Apr 19, 2022
1 parent c9eaf70 commit 1ed24fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
21 changes: 16 additions & 5 deletions Sources/SwiftGraphQL/HTTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,15 @@ private func send<Type, TypeLock>(
}

// Try to serialize the response.
if let data = data, let result = try? GraphQLResult(data, with: selection) {
return completionHandler(.success(result))
if let data = data {
do {
let result = try GraphQLResult(data, with: selection)
return completionHandler(.success(result))
} catch let error as HttpError {
return completionHandler(.failure(error))
} catch let error {
return completionHandler(.failure(.decodingError(error)))
}
}

return completionHandler(.failure(.badpayload))
Expand All @@ -139,16 +146,20 @@ public enum HttpError: Error {
case badpayload
case badstatus
case cancelled
case decodingError(Error)
}

extension HttpError: Equatable {
public static func == (lhs: SwiftGraphQL.HttpError, rhs: SwiftGraphQL.HttpError) -> Bool {
// Equals if they are of the same type, different otherwise.
switch (lhs, rhs) {
case (.badURL, badURL),
(.timeout, .timeout),
(.badpayload, .badpayload),
(.badstatus, .badstatus):
(.timeout, .timeout),
(.badpayload, .badpayload),
(.badstatus, .badstatus),
(.cancelled, .cancelled),
(.network, network),
(.decodingError, decodingError):
return true
default:
return false
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftGraphQL/Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ extension GraphQLResult where TypeLock: Decodable {
let response = try decoder.decode(GraphQLResponse.self, from: response)
self.data = try selection.decode(data: response.data)
self.errors = response.errors
} catch {
} catch let error {
// Catches all errors and turns them into a bad payload SwiftGraphQL error.
throw HttpError.badpayload
throw HttpError.decodingError(error)
}
}

Expand All @@ -46,7 +46,7 @@ extension GraphQLResult where TypeLock: Decodable {
// MARK: - GraphQL Error

public struct GraphQLError: Codable, Equatable {
let message: String
public let message: String
public let locations: [Location]?
// public let path: [String]?

Expand Down

0 comments on commit 1ed24fc

Please sign in to comment.