|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Represents errors that can occur during network operations. |
| 4 | +public enum NetworkError: Throwable { |
| 5 | + /// No internet connection is available. |
| 6 | + /// - Note: This error may occur if the device is in airplane mode or lacks network connectivity. |
| 7 | + case noInternet |
| 8 | + |
| 9 | + /// The request timed out before completion. |
| 10 | + case timeout |
| 11 | + |
| 12 | + /// The server responded with a bad request error. |
| 13 | + /// - Parameters: |
| 14 | + /// - code: The exact HTTP status code returned by the server. |
| 15 | + /// - message: An error message provided by the server in the body. |
| 16 | + case badRequest(code: Int, message: String) |
| 17 | + |
| 18 | + /// The server responded with a general server-side error. |
| 19 | + /// - Parameters: |
| 20 | + /// - code: The HTTP status code returned by the server. |
| 21 | + /// - message: An optional error message provided by the server. |
| 22 | + case serverError(code: Int, message: String?) |
| 23 | + |
| 24 | + /// The response could not be decoded or parsed. |
| 25 | + case decodingFailure |
| 26 | + |
| 27 | + /// Generic error message if the existing cases don't provide the required details. |
| 28 | + case generic(userFriendlyMessage: String) |
| 29 | + |
| 30 | + /// A user-friendly error message suitable for display to end users. |
| 31 | + public var userFriendlyMessage: String { |
| 32 | + switch self { |
| 33 | + case .noInternet: |
| 34 | + return String( |
| 35 | + localized: "BuiltInErrors.NetworkError.noInternet", |
| 36 | + defaultValue: "No internet connection is available. Please check your network settings and try again.", |
| 37 | + bundle: .module |
| 38 | + ) |
| 39 | + case .timeout: |
| 40 | + return String( |
| 41 | + localized: "BuiltInErrors.NetworkError.timeout", |
| 42 | + defaultValue: "The request timed out. Please try again later.", |
| 43 | + bundle: .module |
| 44 | + ) |
| 45 | + case .badRequest(let code, let message): |
| 46 | + return String( |
| 47 | + localized: "BuiltInErrors.NetworkError.badRequest", |
| 48 | + defaultValue: "The request was malformed (\(code)): \(message). Please review and try again.", |
| 49 | + bundle: .module |
| 50 | + ) |
| 51 | + case .serverError(let code, let message): |
| 52 | + let defaultMessage = String( |
| 53 | + localized: "BuiltInErrors.NetworkError.serverError", |
| 54 | + defaultValue: "The server encountered an error (Code: \(code)).", |
| 55 | + bundle: .module |
| 56 | + ) |
| 57 | + if let message = message { |
| 58 | + return defaultMessage + " " + message |
| 59 | + } else { |
| 60 | + return defaultMessage |
| 61 | + } |
| 62 | + case .decodingFailure: |
| 63 | + return String( |
| 64 | + localized: "BuiltInErrors.NetworkError.decodingFailure", |
| 65 | + defaultValue: "The data received from the server could not be processed. Please try again.", |
| 66 | + bundle: .module |
| 67 | + ) |
| 68 | + case .generic(let userFriendlyMessage): |
| 69 | + return userFriendlyMessage |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments