|
1 | 1 | //
|
2 | 2 | // CheckoutNetworkError.swift
|
3 |
| -// |
| 3 | +// |
4 | 4 | //
|
5 | 5 | // Created by Alex Ioja-Yang on 20/06/2022.
|
6 | 6 | //
|
7 | 7 |
|
8 | 8 | import Foundation
|
9 | 9 |
|
10 |
| -public enum CheckoutNetworkError: Error, Equatable { |
11 |
| - |
12 |
| - /// Review the url you provided |
13 |
| - case invalidURL |
14 |
| - |
15 |
| - /// Network response was not in the 200 range |
16 |
| - case unexpectedResponseCode(code: Int) |
17 |
| - |
18 |
| - /// Network call and completion appear valid but no data was returned making the parsing impossible. Use runRequest method with NoDataResponseCompletionHandler if no data is expected (HTTP 204 is a success case with no content being returned) |
19 |
| - case noDataResponseReceived |
20 |
| - |
21 |
| - /// Network response returned with HTTP Code 422 |
22 |
| - case invalidData(reason: ErrorReason) |
| 10 | +public enum CheckoutNetworkError: LocalizedError, Equatable { |
| 11 | + |
| 12 | + /// Review the url you provided |
| 13 | + case invalidURL |
| 14 | + |
| 15 | + /// When a response could not be bound to an instance of HTTPURLResponse |
| 16 | + case invalidURLResponse |
| 17 | + |
| 18 | + /// Network response was not in the 200 range |
| 19 | + case unexpectedHTTPResponse(code: Int) |
| 20 | + |
| 21 | + /// Network call and completion appear valid but no data was returned making the parsing impossible. Use runRequest method with NoDataResponseCompletionHandler if no data is expected (HTTP 204 is a success case with no content being returned) |
| 22 | + case noDataResponseReceived |
| 23 | + |
| 24 | + /// Network response returned with HTTP Code 422 |
| 25 | + case unprocessableContent(reason: ErrorReason) |
| 26 | + |
| 27 | + /// Connectivity errors mapped from URLSession.dataTask()'s error |
| 28 | + /// |
| 29 | + /// Only the following are mapped into this error case: |
| 30 | + /// NSURLErrorNotConnectedToInternet |
| 31 | + /// NSURLErrorTimedOut |
| 32 | + /// NSURLErrorNetworkConnectionLost |
| 33 | + /// NSURLErrorInternationalRoamingOff |
| 34 | + /// NSURLErrorCannotConnectToHost |
| 35 | + /// NSURLErrorServerCertificateUntrusted |
| 36 | + case connectivity |
| 37 | + |
| 38 | + /// All the other errors that can be received from URLSession. |
| 39 | + /// Use the underlying error if you need more granular error handling. |
| 40 | + /// Underlying errors here are in NSURLErrorDomain. |
| 41 | + case other(underlyingError: NSError) |
| 42 | + |
| 43 | + public var errorDescription: String? { |
| 44 | + switch self { |
| 45 | + case .invalidURL: |
| 46 | + return "Could not instantiate a URL with the provided String value" |
| 47 | + |
| 48 | + case .invalidURLResponse: |
| 49 | + return "Could not instantiate an HTTPURLResponse with the received response value" |
| 50 | + |
| 51 | + case .unexpectedHTTPResponse(let code): |
| 52 | + return "Received an unexpected HTTP response: \(code)" |
| 53 | + |
| 54 | + case .noDataResponseReceived: |
| 55 | + return "No data is received in the response body. Use the method with NoDataResponseCompletionHandler if no data was expected" |
| 56 | + |
| 57 | + case .unprocessableContent(let reason): |
| 58 | + return "HTTP response 422 is received with: \(reason)" |
| 59 | + |
| 60 | + case .connectivity: |
| 61 | + return "There is a problem with the internet connection" |
| 62 | + |
| 63 | + case .other(let error): |
| 64 | + return "An unhandled error is produced: \(error)" |
| 65 | + } |
| 66 | + } |
23 | 67 | }
|
0 commit comments