|
| 1 | +import Foundation |
| 2 | +import IONFileTransferLib |
| 3 | + |
| 4 | +/// Represents errors that can occur during file transfer operations. |
| 5 | +/// |
| 6 | +/// The `FileTransferError` enum defines various error cases that may arise |
| 7 | +/// when performing file transfer operations, such as invalid parameters, |
| 8 | +/// connection issues, or permission denial. |
| 9 | +enum FileTransferError: Error { |
| 10 | + |
| 11 | + /// Indicates that the input parameters provided to a method are invalid. |
| 12 | + case invalidParameters |
| 13 | + |
| 14 | + /// Indicates that the provided server URL is invalid. |
| 15 | + /// |
| 16 | + /// - Parameter url: The invalid URL string. |
| 17 | + case invalidServerUrl(url: String) |
| 18 | + |
| 19 | + /// Indicates that the URL to connect to is empty. |
| 20 | + case urlEmpty |
| 21 | + |
| 22 | + /// Indicates that the user denied the required permissions for the operation. |
| 23 | + case permissionDenied |
| 24 | + |
| 25 | + /// Indicates that the file does not exist. |
| 26 | + /// |
| 27 | + /// - Parameter cause: The underlying error, if available. |
| 28 | + case fileDoesNotExist(cause: Error?) |
| 29 | + |
| 30 | + /// Indicates a connection error occurred. |
| 31 | + /// |
| 32 | + /// - Parameter cause: The underlying error, if available. |
| 33 | + case connectionError(cause: Error?) |
| 34 | + |
| 35 | + /// Indicates that the server responded with HTTP 304 – Not Modified. |
| 36 | + /// |
| 37 | + /// - Parameters: |
| 38 | + /// - responseCode: The HTTP response code. |
| 39 | + /// - responseBody: The response body, if available. |
| 40 | + /// - headers: The response headers, if available. |
| 41 | + case notModified(responseCode: Int, responseBody: String?, headers: [String: String]?) |
| 42 | + |
| 43 | + /// Indicates a generic error occurred. |
| 44 | + /// |
| 45 | + /// - Parameter cause: The underlying error, if available. |
| 46 | + case genericError(cause: Error?) |
| 47 | + |
| 48 | + /// A unique error code associated with the error. |
| 49 | + /// |
| 50 | + /// The error code is formatted as `OS-PLUG-FLTR-XXXX`, where `XXXX` is a |
| 51 | + /// numeric identifier for the specific error case. |
| 52 | + var code: String { |
| 53 | + let code: Int |
| 54 | + switch self { |
| 55 | + case .invalidParameters: code = 5 |
| 56 | + case .invalidServerUrl: code = 6 |
| 57 | + case .urlEmpty: code = 6 |
| 58 | + case .permissionDenied: code = 7 |
| 59 | + case .fileDoesNotExist: code = 8 |
| 60 | + case .connectionError: code = 9 |
| 61 | + case .notModified: code = 10 |
| 62 | + case .genericError: code = 11 |
| 63 | + } |
| 64 | + return String(format: "OS-PLUG-FLTR-%04d", code) |
| 65 | + } |
| 66 | + |
| 67 | + /// A human-readable description of the error. |
| 68 | + /// |
| 69 | + /// This property provides a detailed explanation of the error, which can |
| 70 | + /// be used for logging or displaying error messages to the user. |
| 71 | + var description: String { |
| 72 | + switch self { |
| 73 | + case .invalidParameters: "The method's input parameters aren't valid." |
| 74 | + case .invalidServerUrl(url: let url): "Invalid server URL was provided - \(url)" |
| 75 | + case .urlEmpty: "URL to connect to is either null or empty." |
| 76 | + case .permissionDenied: "Unable to perform operation, user denied permission request." |
| 77 | + case .fileDoesNotExist: "Operation failed because file does not exist." |
| 78 | + case .connectionError: "Failed to connect to server." |
| 79 | + case .notModified: "The server responded with HTTP 304 – Not Modified. If you want to avoid this, check your headers related to HTTP caching." |
| 80 | + case .genericError: "The operation failed with an error." |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// The underlying cause of the error, if available. |
| 85 | + /// |
| 86 | + /// This property provides additional context for the error, such as the |
| 87 | + /// original error that triggered the current error. |
| 88 | + var cause: Error? { |
| 89 | + switch self { |
| 90 | + case .invalidParameters: return nil |
| 91 | + case .invalidServerUrl: return nil |
| 92 | + case .urlEmpty: return nil |
| 93 | + case .permissionDenied: return nil |
| 94 | + case .fileDoesNotExist(cause: let cause): return cause |
| 95 | + case .connectionError(cause: let cause): return cause |
| 96 | + case .notModified: return nil |
| 97 | + case .genericError(cause: let cause): return cause |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// Extension to map `IONFLTRException` to `FileTransferError`. |
| 103 | +extension IONFLTRException { |
| 104 | + |
| 105 | + /// Converts an `IONFLTRException` to a corresponding `FileTransferError`. |
| 106 | + /// |
| 107 | + /// This method maps specific cases of `IONFLTRException` to their |
| 108 | + /// equivalent `FileTransferError` cases, providing a unified error |
| 109 | + /// representation for file transfer operations. |
| 110 | + /// |
| 111 | + /// - Returns: A `FileTransferError` instance representing the exception. |
| 112 | + func toFileTransferError() -> FileTransferError { |
| 113 | + switch self { |
| 114 | + case .invalidPath(_): |
| 115 | + return .invalidParameters |
| 116 | + case .emptyURL(_): |
| 117 | + return .urlEmpty |
| 118 | + case .invalidURL(let url): |
| 119 | + return .invalidServerUrl(url: url) |
| 120 | + case .fileDoesNotExist(let cause): |
| 121 | + return .fileDoesNotExist(cause: cause) |
| 122 | + case .cannotCreateDirectory(_, let cause): |
| 123 | + return .genericError(cause: cause) |
| 124 | + case .httpError(let responseCode, let responseBody, let headers): |
| 125 | + return responseCode == 304 |
| 126 | + ? .notModified( |
| 127 | + responseCode: responseCode, |
| 128 | + responseBody: responseBody, |
| 129 | + headers: headers |
| 130 | + ) : .genericError(cause: nil) |
| 131 | + case .connectionError(let cause): |
| 132 | + return .connectionError(cause: cause) |
| 133 | + case .transferError(let cause): |
| 134 | + return .genericError(cause: cause) |
| 135 | + case .unknownError(let cause): |
| 136 | + return .genericError(cause: cause) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments