Swift / Error handling

Swift: record safe error diagnostics

Reduce Swift SDK errors to a fixed category and bounded HTTP status without recording keys, bodies or customer data.

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.

Log a small, explicit diagnostic record

Raw error messages and response bodies can contain request or customer details. This example creates a record with only an app-owned category and an optional HTTP status. It performs no logging itself.

import VedikaSDK

enum DiagnosticKind: String {
    case configuration, authentication, balance
    case rateLimit, server, request, unexpected
}

struct SafeDiagnostic {
    let kind: DiagnosticKind
    let statusCode: Int?
}

func productionDiagnostic(for error: Error) -> SafeDiagnostic {
    let kind: DiagnosticKind
    switch error {
    case is VedikaConfigurationError: kind = .configuration
    case is VedikaAuthError: kind = .authentication
    case is VedikaInsufficientCredits: kind = .balance
    case is VedikaRateLimitError: kind = .rateLimit
    case is VedikaServerError: kind = .server
    case is VedikaApiError: kind = .request
    default: kind = .unexpected
    }

    let status: Int?
    if let value = (error as? VedikaApiError)?.statusCode,
       (100...599).contains(value) {
        status = value
    } else {
        status = nil
    }
    return SafeDiagnostic(kind: kind, statusCode: status)
}

Keep the logger boundary explicit

If you record this result, send only kind.rawValue and statusCode to your approved logger. Do not add the original Error, message, body, API key, URL parameters, or customer input. HTTP 403 remains the generic request category with status 403.

Handle unknown outcomes with care

A network failure can arrive without an HTTP status. Preserve that absence rather than labeling it as a server response. The SDK already retries network failures and 5xx responses internally; this diagnostic function never retries, queues, or resends work.

Use the record to find a failure class. It is not evidence that a request was unbilled, refunded, or safely repeatable. Confirm the request result through your account workflow.

Continue with API documentation, current pricing, or support.