Skip to content

Commit

Permalink
Add additional debugging info when JSON decoding fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Xie committed Nov 16, 2016
1 parent 5a26a84 commit 11dbccc
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 deletions Sources/GraphQLMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,54 @@ public struct GraphQLMap {
}

public func value<T>(forKey key: String, decoder: JSONDecoder<T>) throws -> T {
return try decoder(jsonValue(forKey: key))
do {
return try decoder(jsonValue(forKey: key))
} catch let error as JSONDecodingError {
throw GraphQLMapDecodingError(error: error, key: key, localizedJson: jsonObject)
}
}

public func value<T: JSONDecodable>(forKey key: String) throws -> T {
return try value(forKey: key, decoder: T.init(jsonValue:))
do {
return try value(forKey: key, decoder: T.init(jsonValue:))
} catch let error as JSONDecodingError {
throw GraphQLMapDecodingError(error: error, key: key, localizedJson: jsonObject)
}
}

public func optionalValue<T: JSONDecodable>(forKey key: String) throws -> T? {
guard let jsonValue = optionalJSONValue(forKey: key) else { return nil }
return try Optional<T>.init(jsonValue: jsonValue)
do {
return try Optional<T>.init(jsonValue: jsonValue)
} catch let error as JSONDecodingError {
throw GraphQLMapDecodingError(error: error, key: key, localizedJson: jsonObject)
}
}

public func list<T>(forKey key: String, decoder: JSONDecoder<T>) throws -> [T] {
let value = try jsonValue(forKey: key)
guard let array = value as? JSONArray else {
throw JSONDecodingError.couldNotConvert(value: value, to: JSONArray.self)
do {
let value = try jsonValue(forKey: key)
guard let array = value as? JSONArray else {
throw JSONDecodingError.couldNotConvert(value: value, to: JSONArray.self)
}
return try array.map { try decoder($0) }
} catch let error as JSONDecodingError {
throw GraphQLMapDecodingError(error: error, key: key, localizedJson: jsonObject)
}
return try array.map { try decoder($0) }
}

public func optionalList<T>(forKey key: String, decoder: JSONDecoder<T>) throws -> [T]? {
guard let value = optionalJSONValue(forKey: key) else { return nil }
if value is NSNull { return nil }

guard let array = value as? JSONArray else {
throw JSONDecodingError.couldNotConvert(value: value, to: JSONArray.self)
do {
guard let array = value as? JSONArray else {
throw JSONDecodingError.couldNotConvert(value: value, to: JSONArray.self)
}
return try array.map { try decoder($0) }
} catch let error as JSONDecodingError {
throw GraphQLMapDecodingError(error: error, key: key, localizedJson: jsonObject)
}
return try array.map { try decoder($0) }
}

public func list<T: JSONDecodable>(forKey key: String) throws -> [T] {
Expand Down Expand Up @@ -146,3 +166,20 @@ public extension GraphQLMapEncodable {
return graphQLMap.jsonValue
}
}

public struct GraphQLMapDecodingError: Error, LocalizedError {

public let error: JSONDecodingError
public let key: String
public let localizedJson: JSONObject

init(error: JSONDecodingError, key: String, localizedJson: JSONObject) {
self.error = error
self.key = key
self.localizedJson = localizedJson
}

public var errorDescription: String? {
return error.errorDescription
}
}

0 comments on commit 11dbccc

Please sign in to comment.