Skip to content

Instantly share code, notes, and snippets.

@chrisvasselli
Created December 20, 2022 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvasselli/fc06aaf4a2c7e3e1e7eddf6a65867797 to your computer and use it in GitHub Desktop.
Save chrisvasselli/fc06aaf4a2c7e3e1e7eddf6a65867797 to your computer and use it in GitHub Desktop.
Check if an error is related to network conditions outside your control
extension Error {
var code: Int { return (self as NSError).code }
var domain: String { return (self as NSError).domain }
var userInfo: [String:Any] { return (self as NSError).userInfo }
var isRelatedToPoorInternetConnection: Bool {
switch self {
case URLError.timedOut,
URLError.cancelled,
URLError.dataNotAllowed,
URLError.notConnectedToInternet,
URLError.secureConnectionFailed,
URLError.cannotParseResponse,
URLError.cannotConnectToHost,
URLError.networkConnectionLost,
URLError.internationalRoamingOff,
URLError.cannotFindHost,
CKError.networkFailure,
CKError.networkUnavailable:
return true
default:
switch (domain, code) {
case ("kCFErrorDomainCFNetwork", Int(CFNetworkErrors.cfErrorHTTPParseFailure.rawValue)),
("_OnDemandResourcesErrorDomain", 2008): // Error fetching manifest, invalid response received: (null)
return true
default:
return false
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment