|
| 1 | +import Foundation |
| 2 | +#if canImport(FoundationNetworking) |
| 3 | +import FoundationNetworking |
| 4 | +#endif |
| 5 | + |
| 6 | +extension ErrorKit { |
| 7 | + static func enhancedFoundationDescription(for error: Error) -> String? { |
| 8 | + switch error { |
| 9 | + |
| 10 | + // URLError: Networking errors |
| 11 | + case let urlError as URLError: |
| 12 | + switch urlError.code { |
| 13 | + case .notConnectedToInternet: |
| 14 | + return String( |
| 15 | + localized: "CommonErrors.URLError.notConnectedToInternet", |
| 16 | + defaultValue: "You are not connected to the Internet. Please check your connection.", |
| 17 | + bundle: .module |
| 18 | + ) |
| 19 | + case .timedOut: |
| 20 | + return String( |
| 21 | + localized: "CommonErrors.URLError.timedOut", |
| 22 | + defaultValue: "The request timed out. Please try again later.", |
| 23 | + bundle: .module |
| 24 | + ) |
| 25 | + case .cannotFindHost: |
| 26 | + return String( |
| 27 | + localized: "CommonErrors.URLError.cannotFindHost", |
| 28 | + defaultValue: "Unable to find the server. Please check the URL or your network.", |
| 29 | + bundle: .module |
| 30 | + ) |
| 31 | + case .networkConnectionLost: |
| 32 | + return String( |
| 33 | + localized: "CommonErrors.URLError.networkConnectionLost", |
| 34 | + defaultValue: "The network connection was lost. Please try again.", |
| 35 | + bundle: .module |
| 36 | + ) |
| 37 | + default: |
| 38 | + return String( |
| 39 | + localized: "CommonErrors.URLError.default", |
| 40 | + defaultValue: "A network error occurred: \(urlError.localizedDescription)", |
| 41 | + bundle: .module |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + // CocoaError: File-related errors |
| 46 | + case let cocoaError as CocoaError: |
| 47 | + switch cocoaError.code { |
| 48 | + case .fileNoSuchFile: |
| 49 | + return String( |
| 50 | + localized: "CommonErrors.CocoaError.fileNoSuchFile", |
| 51 | + defaultValue: "The file could not be found.", |
| 52 | + bundle: .module |
| 53 | + ) |
| 54 | + case .fileReadNoPermission: |
| 55 | + return String( |
| 56 | + localized: "CommonErrors.CocoaError.fileReadNoPermission", |
| 57 | + defaultValue: "You do not have permission to read this file.", |
| 58 | + bundle: .module |
| 59 | + ) |
| 60 | + case .fileWriteOutOfSpace: |
| 61 | + return String( |
| 62 | + localized: "CommonErrors.CocoaError.fileWriteOutOfSpace", |
| 63 | + defaultValue: "There is not enough disk space to complete the operation.", |
| 64 | + bundle: .module |
| 65 | + ) |
| 66 | + default: |
| 67 | + return String( |
| 68 | + localized: "CommonErrors.CocoaError.default", |
| 69 | + defaultValue: "A file system error occurred: \(cocoaError.localizedDescription)", |
| 70 | + bundle: .module |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + // POSIXError: POSIX errors |
| 75 | + case let posixError as POSIXError: |
| 76 | + switch posixError.code { |
| 77 | + case .ENOSPC: |
| 78 | + return String( |
| 79 | + localized: "CommonErrors.POSIXError.ENOSPC", |
| 80 | + defaultValue: "There is no space left on the device.", |
| 81 | + bundle: .module |
| 82 | + ) |
| 83 | + case .EACCES: |
| 84 | + return String( |
| 85 | + localized: "CommonErrors.POSIXError.EACCES", |
| 86 | + defaultValue: "Permission denied. Please check your file permissions.", |
| 87 | + bundle: .module |
| 88 | + ) |
| 89 | + case .EBADF: |
| 90 | + return String( |
| 91 | + localized: "CommonErrors.POSIXError.EBADF", |
| 92 | + defaultValue: "Bad file descriptor. The file may be closed or invalid.", |
| 93 | + bundle: .module |
| 94 | + ) |
| 95 | + default: |
| 96 | + return String( |
| 97 | + localized: "CommonErrors.POSIXError.default", |
| 98 | + defaultValue: "A system error occurred: \(posixError.localizedDescription)", |
| 99 | + bundle: .module |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + default: |
| 104 | + return nil |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments