Skip to content

Commit 4b9a36b

Browse files
authored
Merge pull request #47 from GoodRequest/implementation-improvements
Small documentation improvements
2 parents 781e523 + ccef269 commit 4b9a36b

1 file changed

Lines changed: 38 additions & 12 deletions

File tree

Sources/GoodNetworking/Session/NetworkError.swift

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,27 @@ import Foundation
99

1010
// MARK: - Network error
1111

12-
/// Top level error, which can occur in all networking operations in this library.
13-
///
14-
/// The error is organized as follows:
15-
/// - Local errors (`URLError`): errors which affect only the local state. May contain
16-
/// failed networking operations, no connection errors, invalid URL errors etc.
17-
/// - Remote errors (``HTTPError``): errors which occured as a result of invalid operation
18-
/// over remote state. This contains all HTTP errors, invalid API calls etc., but also means that
19-
/// the request itself on network level has succeeded.
20-
/// - Decoding errors (`DecodingError`): errors which occured during decoding. The request
21-
/// has succeeded, returned a valid, success, response, but could not be decoded to a valid
22-
/// data type in the client.
12+
/// Top level error, which can occur in all networking operations in GoodNetworking.
13+
///
14+
/// All underlying errors are fall into three categories, which are represented
15+
/// by respective enum cases into local errors, remote errors and coding errors.
16+
///
17+
/// - Local errors (`URLError`)
18+
/// - Remote errors (``HTTPError``)
19+
/// - Decoding errors (`DecodingError`)
2320
public enum NetworkError: LocalizedError {
2421

22+
/// Errors which affect only the local state. May include failed networking
23+
/// operations, no connection errors, invalid URL errors etc.
2524
case local(URLError)
25+
26+
/// Errors which occured as a result of invalid operation over remote state.
27+
/// This contains all HTTP errors, invalid API calls etc., but also means
28+
/// the request has succeeded on the network layer.
2629
case remote(HTTPError)
30+
31+
/// Errors which occured during decoding. The request has succeeded
32+
/// with a valid response, but could not be decoded to any data type in the client.
2733
case decoding(DecodingError)
2834

2935
public var errorDescription: String? {
@@ -38,6 +44,12 @@ public enum NetworkError: LocalizedError {
3844
return decodingError.localizedDescription
3945
}
4046
}
47+
48+
}
49+
50+
// MARK: - Network error extensions
51+
52+
extension NetworkError {
4153

4254
/// HTTP status code, if the error is a `remote` ``HTTPError``, or `nil` otherwise.
4355
///
@@ -50,7 +62,21 @@ public enum NetworkError: LocalizedError {
5062
return nil
5163
}
5264
}
53-
65+
66+
/// Attempts decoding failure response as a decodable error structure.
67+
///
68+
/// If the network error is ``local(_:)`` or ``decoding(_:)`` error,
69+
/// this function returns `nil`.
70+
/// If the network error is ``remote(_:)``, response data is decoded as `T`
71+
/// using JSON decoder.
72+
public func remote<T: Decodable & Error>(as errorType: T.Type) -> T? {
73+
if case .remote(let httpError) = self {
74+
return try? JSONDecoder().decode(T.self, from: httpError.errorResponse)
75+
} else {
76+
return nil
77+
}
78+
}
79+
5480
}
5581

5682
// MARK: - Local error extensions

0 commit comments

Comments
 (0)