Describe all possible paths in your program with enum. This is great to track down bugs and to not miss representing potential cases in UI. Errors can come from app layer, backend layer to network issues.
Enum is handy in both UIKit and SwiftUI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
enumNetworkError: Swift.Error{ enumRequestError{ case invalidRequest(URLRequest) case encodingError(Swift.EncodingError) case other(NSError) }
enumServerError{ case decodingError(Swift.DecodingError) case noInternetConnection case timeout case internalServerError case other(statusCode: Int, response: HTTPURLResponse) }
case requestError(RequestError) case serverError(ServerError) }
and note that Foundation has NSError with a bunch of well defined error code ready to use. We can use this before introduce our custom error cases
publicvarNSURLErrorUnknown: Int { get } publicvarNSURLErrorCancelled: Int { get } publicvarNSURLErrorBadURL: Int { get } publicvarNSURLErrorTimedOut: Int { get } publicvarNSURLErrorUnsupportedURL: Int { get } publicvarNSURLErrorCannotFindHost: Int { get } publicvarNSURLErrorCannotConnectToHost: Int { get } publicvarNSURLErrorNetworkConnectionLost: Int { get } publicvarNSURLErrorDNSLookupFailed: Int { get } publicvarNSURLErrorHTTPTooManyRedirects: Int { get } publicvarNSURLErrorResourceUnavailable: Int { get } publicvarNSURLErrorNotConnectedToInternet: Int { get } publicvarNSURLErrorRedirectToNonExistentLocation: Int { get } publicvarNSURLErrorBadServerResponse: Int { get } publicvarNSURLErrorUserCancelledAuthentication: Int { get } publicvarNSURLErrorUserAuthenticationRequired: Int { get } publicvarNSURLErrorZeroByteResource: Int { get } publicvarNSURLErrorCannotDecodeRawData: Int { get } publicvarNSURLErrorCannotDecodeContentData: Int { get } publicvarNSURLErrorCannotParseResponse: Int { get }