Skip to content

Commit

Permalink
fix #34: the decode() function when throws due to fetch() error will …
Browse files Browse the repository at this point in the history
…dispatch received error from network outside
  • Loading branch information
malcommac committed Mar 23, 2022
1 parent f00cf51 commit f74af10
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/RealHTTP/Client/HTTPResponse/HTTPResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public struct HTTPResponse: CustomStringConvertible {
///
/// - Returns: `T` or `nil` if no response has been received.
public func decode<T: Decodable>(_ decodable: T.Type, decoder: JSONDecoder = .init()) throws -> T {
if let error = error {
throw error // dispatch any error coming from fetch outside the decode.
}

guard let data = data else {
throw HTTPError.init(.emptyResponse)
}
Expand All @@ -154,6 +158,11 @@ public struct HTTPResponse: CustomStringConvertible {
/// - Returns: T?
public func decodeJSONData<T>(_ decodable: T.Type,
options: JSONSerialization.ReadingOptions = []) throws -> T? {

if let error = error {
throw error // dispatch any error coming from fetch outside the decode.
}

guard let data = data else { return nil }

let object = try JSONSerialization.jsonObject(with: data, options: options)
Expand Down

0 comments on commit f74af10

Please sign in to comment.