-
Notifications
You must be signed in to change notification settings - Fork 1
Error Handling
Every failure that crosses the public boundary is a NetworkError:
NetworkError
├── invalidURL(String)
├── noInternet
├── timeout
├── unauthorized(ResponseContext)
├── forbidden(ResponseContext)
├── notFound(ResponseContext)
├── validation(ResponseContext)
├── rateLimited(retryAfter: TimeInterval?, ResponseContext)
├── server(ResponseContext)
├── unacceptableStatusCode(Int, ResponseContext)
├── decoding(underlying: Error, ResponseContext?)
├── encoding(underlying: Error)
├── sslPinningFailed(host: String)
├── tokenRefreshFailed(underlying: Error)
├── sessionExpired
├── cancelled
├── offline
├── offlineQueued(RequestID)
├── transport(underlying: Error)
└── unknown(underlying: Error?)
Switch on the stable code discriminant, and read the response context when present:
do {
let user = try await client.request(GetProfile())
} catch let error as NetworkError {
switch error.code {
case .unauthorized, .sessionExpired:
await AppRouter.logout()
case .notFound:
showNotFound()
case .validation:
show(error.serverMessage ?? "Invalid input") // decoded from the response body
case .noInternet, .offline:
showOfflineBanner()
case .rateLimited:
// error.responseHeaders?["Retry-After"]
scheduleRetryLater()
default:
showGeneric(error)
}
// error.statusCode, error.responseHeaders, error.responseData also available
}NetworkError.normalize(_:) maps an arbitrary thrown error into this type if you catch something
loosely typed.
Important:
NetworkError'serrorDescriptioncan include text taken from the server's response and from underlying errors, and is not passed throughRedactor. Treat descriptions as untrusted when logging or reporting; usecodeandstatusCodewhere a description could leak something sensitive. Credentials an OAuth token endpoint echoes back into an error body are masked, but that's the one place this is handled automatically.
SwiftNetworkKit is source-available proprietary software (not open source). See FAQ Licensing and Support. Repo: https://github.com/ihusnainalii/SwiftNetworkKit · Docs: https://swiftnetworkkit.vercel.app/
Getting started
Guides
- Defining Endpoints
- Authentication and Token Refresh
- Security and Certificate Pinning
- Retry Policy
- Interceptors
- Request Management
- Caching
- Offline Request Queue
- Uploads Downloads and Progress
- Pagination and Batch
- Combine and SwiftUI
- Error Handling
- Observability
- Testing and Mocking
- Best Practices and a Complete Example
- Platform Notes, Thread Safety and Performance
Reference