Swift / Error handling

Swift: handle rate-limit advice without a retry loop

Read the optional Retry-After seconds from VedikaRateLimitError and keep retry decisions under app control.

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.

Treat the wait hint as optional

HTTP 429 carries a typed error. The current client parses Retry-After as an integer number of seconds. A missing, malformed, or date-form header gives no parsed value. This helper also rejects a negative value.

import VedikaSDK

struct RetryAdvice: Equatable {
    let waitSeconds: Int?
    let message: String
}

func advancedRetryAdvice(for error: Error) -> RetryAdvice {
    guard let limit = error as? VedikaRateLimitError else {
        return RetryAdvice(
            waitSeconds: nil,
            message: "Review the request outcome before trying again."
        )
    }
    guard let seconds = limit.retryAfterSeconds, seconds >= 0 else {
        return RetryAdvice(
            waitSeconds: nil,
            message: "Rate limit reached. No valid wait time was supplied."
        )
    }
    return RetryAdvice(
        waitSeconds: seconds,
        message: "Rate limit reached. Wait at least \(seconds) seconds."
    )
}

Separate waiting from sending

Use waitSeconds to explain a wait in your UI. A value of zero is valid advice; it is not a promise that the next request will succeed. When the value is absent, do not invent a reset time. This helper never sleeps or sends a request.

Know the client’s existing retries

The current VedikaClient makes up to two additional attempts for network failures and 5xx responses. Its delays are 250 ms and 500 ms. It does not automatically retry rejected 4xx responses, including 429.

The client reuses the same request and idempotency key within those internal attempts. A separate service call can create a new key. Do not wrap calls in an outer automatic retry loop; review uncertain paid outcomes before a new call.

Continue with API documentation, current pricing, or support.