Error handling in the iOS SDK

Handle OctoError in the Swift SDK: the error cases, how they map to API error codes, and when to retry a failed request.

Working draft. These docs are illustrative: names, endpoints and examples describe the intended behaviour and can change before launch.

Every failure in the iOS SDK is an OctoError. It is an enum that conforms to Error and LocalizedError, so you can catch it in a do block around an async call or around a for try await loop. Match on the case to decide what to show the customer.

The OctoError type

public enum OctoError: Error, LocalizedError, Sendable {
    case invalidRequest(String, requestId: String?)
    case unauthorized(String, requestId: String?)
    case forbidden(String, requestId: String?)
    case notFound(String, requestId: String?)
    case conflict(String, requestId: String?)
    case rateLimited(String, requestId: String?, retryAfter: TimeInterval?)
    case server(String, requestId: String?)
    case network(String)

    public var requestId: String? { get }
}
Case API code HTTP status Typical cause
.invalidRequest invalid_request 400 Empty message or a malformed conversation id.
.unauthorized unauthorized 401 Wrong agent id or an invalid userHash.
.forbidden forbidden 403 The agent is not published or not visible to this caller.
.notFound not_found 404 The conversation or message does not exist.
.conflict conflict 409 The conversation changed state while you sent a message.
.rateLimited rate_limited 429 Too many requests. retryAfter comes from the Retry-After header, in seconds.
.server server_error 500 A problem on the server side.
.network none none No connection, timeout or interrupted stream.

requestId matches the requestId in the API error body. Include it when you contact support. The codes are the same as in API error handling.

Catch errors

do {
    let response = try await octo.chat(text: input)
    show(response.message.content)
} catch OctoError.rateLimited(_, _, let retryAfter) {
    try? await Task.sleep(nanoseconds: UInt64((retryAfter ?? 5) * 1_000_000_000))
    await retry()
} catch OctoError.network {
    showBanner("You seem to be offline.")
} catch let error as OctoError {
    showBanner("Something went wrong. Reference: \(error.requestId ?? "none")")
}

List the specific cases first and the general OctoError pattern last. A task that is cancelled throws Swift's CancellationError. Do not treat that as a failure, and do not retry it.

Retry rules

  • Retry .network, .server and .rateLimited. Wait for retryAfter when it is set, and use a growing delay otherwise.
  • Do not retry .invalidRequest, .unauthorized, .forbidden and .notFound. Fix the cause first.
  • Reload after .conflict. Load the conversation with conversation(id:) and try again.

Note: A retried chat call can create a second user message if the first request reached the server but the response was lost. Ask the customer before you resend, or load the messages first.

Errors in streams

A stream can fail in two ways. Before the first byte, the for try await loop throws the OctoError. After the stream has started, it emits a .error event. Handle both, as shown in Streaming events.

  • Client lists the request timeout that produces .network errors.
  • User identity explains the .unauthorized case for hashes.

Something unclear or missing? Tell us and we will fix the page.