diff options
| author | Christian Cleberg <[email protected]> | 2026-03-19 16:59:16 -0500 |
|---|---|---|
| committer | Christian Cleberg <[email protected]> | 2026-03-19 16:59:16 -0500 |
| commit | b82bbeeea48ad27832a355c2a41408559c411104 (patch) | |
| tree | 0af45101300c75970471573b09f878a1b3763b38 /Hutch/Networking/SRHTError.swift | |
| parent | 9065ef6e92245a44390e336cc1ae515ae706cdd7 (diff) | |
| download | hutch-b82bbeeea48ad27832a355c2a41408559c411104.tar.gz hutch-b82bbeeea48ad27832a355c2a41408559c411104.tar.bz2 hutch-b82bbeeea48ad27832a355c2a41408559c411104.zip | |
v2.1: bundled polish and fixes
Diffstat (limited to 'Hutch/Networking/SRHTError.swift')
| -rw-r--r-- | Hutch/Networking/SRHTError.swift | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/Hutch/Networking/SRHTError.swift b/Hutch/Networking/SRHTError.swift index f2da408..3c3a87f 100644 --- a/Hutch/Networking/SRHTError.swift +++ b/Hutch/Networking/SRHTError.swift @@ -33,6 +33,55 @@ enum SRHTError: LocalizedError, Sendable { } } + var userFacingMessage: String { + switch self { + case .graphQLErrors(let errors): + let firstMessage = errors.first?.message.lowercased() ?? "" + if firstMessage.contains("unauthorized") || firstMessage.contains("forbidden") { + return "You do not have permission to do that." + } + if firstMessage.contains("not found") || firstMessage.contains("no rows in result set") { + return "That content is no longer available." + } + return "Something went wrong. Please try again." + case .httpError(let code): + if code == 401 { + return "Please sign in again." + } + if code == 403 { + return "You do not have permission to do that." + } + if code == 404 { + return "That content is no longer available." + } + if (500...599).contains(code) { + return "The server is unavailable right now. Please try again." + } + return "Something went wrong. Please try again." + case .invalidAuthenticatedURL: + return "That request could not be completed." + case .decodingError: + return "The response could not be loaded right now." + case .networkError(let error): + let nsError = error as NSError + switch nsError.code { + case NSURLErrorNotConnectedToInternet, + NSURLErrorNetworkConnectionLost, + NSURLErrorTimedOut, + NSURLErrorCannotFindHost, + NSURLErrorCannotConnectToHost, + NSURLErrorDNSLookupFailed, + NSURLErrorInternationalRoamingOff, + NSURLErrorDataNotAllowed: + return "Check your connection and try again." + default: + return "The network request failed. Please try again." + } + case .unauthorized: + return "Please sign in again." + } + } + /// Whether this error represents a connectivity issue (no internet, timeout, DNS). var isConnectivityError: Bool { switch self { @@ -55,6 +104,29 @@ enum SRHTError: LocalizedError, Sendable { } } +extension Error { + var userFacingMessage: String { + if let error = self as? SRHTError { + return error.userFacingMessage + } + + let nsError = self as NSError + switch nsError.code { + case NSURLErrorNotConnectedToInternet, + NSURLErrorNetworkConnectionLost, + NSURLErrorTimedOut, + NSURLErrorCannotFindHost, + NSURLErrorCannotConnectToHost, + NSURLErrorDNSLookupFailed, + NSURLErrorInternationalRoamingOff, + NSURLErrorDataNotAllowed: + return "Check your connection and try again." + default: + return "Something went wrong. Please try again." + } + } +} + /// A single error entry from the GraphQL `errors` array. struct GraphQLError: Decodable, Sendable { let message: String |
