Swift / Error handling
Swift: a minimal error boundary
Catch a typed authentication error and return safe, fixed messages from one Swift request boundary.
These examples use the local VedikaSDK package at sdks/swift in the Vedika repository. The current Swift client covers Vastu. The package requires Swift 5.9, iOS 15 or macOS 12.
Catch the specific type first
Use this small boundary when the caller needs a result message, not a retry policy. The request closure is supplied by your app. Defining this function sends no request.
import VedikaSDK
func minimalMessage(
_ request: () async throws -> Void
) async -> String {
do {
try await request()
return "Request completed."
} catch is VedikaAuthError {
return "Check the API key configuration."
} catch let failure as VedikaApiError {
if let status = failure.statusCode {
return "Request failed (HTTP \(status))."
}
return "Request failed without an HTTP status."
} catch {
return "The request could not complete."
}
}
Why catch order matters
VedikaAuthError is a subclass of VedikaApiError. Put the specific catch first or the general catch will handle it. HTTP 401 maps to the authentication class. Other API errors reach the general branch in this minimal example.
Keep the boundary small
The final catch also handles errors outside that class hierarchy. Return a fixed message; do not display or log the raw error body, key, or customer input. This function makes one logical call and adds no automatic retry loop.
To check the failure branch without a network call, supply a closure that throws VedikaAuthError("fixture"). The result is the fixed configuration message.
Continue with API documentation, current pricing, or support.