Skip to content
QwryAI
Documentation
Pages

iOS SDK

Error Handling

QwryError, the codes it carries, and patterns for handling them in the iOS SDK

Every method on QwryClient that can fail throws one type, QwryError. The only other error you'll see is Swift's own CancellationError, when you cancel the task that's waiting on the SDK.

The error type

QwryError covers both what the server refuses and what the SDK finds on its own:

CaseCovers
.api(status:code:message:details:)The QwryAI server refused the request, or an answer failed on the server.
.network(String)The request never got an answer: no connection, a DNS failure or a timeout.
.invalidResponseThe server answered with something the SDK couldn't read.
.toolLoopLimitExceededThe agent called tools more times in a row than maxToolLoopSteps allows.

Every case has a readable localizedDescription. Keep a final catch for anything else, such as a CancellationError.

QwryError

Swift
public enum QwryError: Error, Sendable, Equatable {
    case api(status: Int, code: String, message: String, details: [String: String]?)
    case network(String)
    case invalidResponse
    case toolLoopLimitExceeded
}
CaseDescription
api(status:code:message:details:)The HTTP status, a stable code to check in your app, a message written for developers, and details, which names the fields that were wrong when the server says.
network(String)The system's description of what went wrong with the connection. Treat it as "you appear to be offline, try again".
invalidResponseThe response couldn't be read. Rare. Treat it like a network problem, and update to the latest SDK if it keeps happening.
toolLoopLimitExceededSee Client-Side Tools.

Shortcuts

Swift
public var isRateLimited: Bool       // status 429
public var isNotFound: Bool          // status 404
public var isCreditsExhausted: Bool  // status 402
Swift
catch let error as QwryError {
    if error.isCreditsExhausted { showOutOfCredits() }
    else if error.isRateLimited { await backOff() }
    else { showGenericError() }
}

If an error response can't be read, the SDK still throws .api with the real status and the code INTERNAL_SERVER_ERROR, so you never lose the status code.

Error codes

These are the codes you can get in .api(status:code:message:details:):

StatusCodeDescription
400VALIDATION_INVALID_BODYThe request wasn't valid. details says which field. You also get this when a tool result is over 20 KB, or a page cursor isn't valid.
400VALIDATION_INVALID_JSONThe request body wasn't valid JSON.
400VALIDATION_MISSING_USER_IDENTIFIERThe device ID header was missing. You shouldn't see this; the SDK always sends it.
400AUTH_INVALID_USER_AGENTThe SDK's User-Agent header was missing or changed. You should only see this if something rewrites your headers.
400CHAT_RETRY_NO_USER_MESSAGEThe message you passed to retry has no user message before it to answer.
401AUTH_INVALID_JWTThe token you passed to identify(token:), or the saved token on a later request, is invalid or expired. You also get this when the agent has no identity verification secret. See User Identity.
402CHAT_CREDITS_EXHAUSTEDThe workspace has no message credits left. Upgrade the plan, add credits, or wait for the monthly reset.
403AUTH_OWNERSHIP_MISMATCHThe conversation belongs to a different user or device, or doesn't exist.
403CHAT_CONVERSATION_MISMATCHThe conversation belongs to a different agent.
403CHAT_CONVERSATION_NOT_ONGOINGThe conversation has ended or a person has taken it over, or it's still busy with another answer or a pending tool. Start a new conversation, or wait and try again.
403ACCESS_COUNTRY_BLOCKEDThe agent blocks requests from the device's country.
404AGENT_NOT_FOUNDNo agent has that ID, or the iOS SDK channel's Visibility is off. See Quick Start.
404RESOURCE_NOT_FOUNDThe address the SDK called doesn't exist.
404CHAT_RETRY_MESSAGE_NOT_FOUNDThe message ID you gave retry isn't an agent message in that conversation.
404RESOURCE_TOOL_CALL_NOT_FOUNDThe tool call wasn't found, or it expired. This can come up during the tool loop.
404RESOURCE_TOOL_CALL_MISMATCHThe tool call belongs to a different conversation.
404RESOURCE_TOOL_RESULT_NOT_PENDINGThe server wasn't waiting for this tool result. Usually it was sent twice.
429RATE_LIMIT_TOO_MANY_REQUESTSToo many requests: the limit is 1,000 every 10 seconds per device and agent. Wait and try again.
500CHAT_STREAMING_ERRORThe answer failed on the server while streaming. Safe to try again.
500INTERNAL_SERVER_ERRORSomething went wrong on the server. Try again, or contact support if it keeps happening.

Handling errors

Swift
do {
    let response = try await client.send("Hello")
    render(response)

} catch let error as QwryError {
    switch error {
    case .api(401, _, _, _):
        try await refreshIdentity()
    case .api(402, _, _, _):
        show("You're out of message credits.")
    case .api(403, "CHAT_CONVERSATION_NOT_ONGOING", _, _):
        await client.newConversation()
    case .api(404, "AGENT_NOT_FOUND", _, _):
        assertionFailure("Check the agent ID and that the iOS SDK channel is visible")
    case .api(429, _, _, _):
        await backOff()
    case .toolLoopLimitExceeded:
        show("That took too many steps. Try rewording it.")
    case .network:
        show("You appear to be offline. Try again.")
    default:
        show("Something went wrong. Please try again.")
    }

} catch is CancellationError {
    // The user stopped it. Nothing to show.

} catch {
    show("Something went wrong. Please try again.")
}

Errors inside tool handlers

An error thrown by a tool handler does not come out of send. The SDK turns it into {"error": "..."} and gives it to the agent, so the agent can recover. See When a tool fails.

What to report

When you contact support about an error, include the status, the code and the time it happened. That's enough to find the request on our side.

Explore the documentation