# 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: | Case | Covers | | - | - | | `.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. | | `.invalidResponse` | The server answered with something the SDK couldn't read. | | `.toolLoopLimitExceeded` | The 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 } ``` | Case | Description | | - | - | | `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". | | `invalidResponse` | The response couldn't be read. Rare. Treat it like a network problem, and update to the latest SDK if it keeps happening. | | `toolLoopLimitExceeded` | See [Client-Side Tools](/docs/ios-sdk/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() } } ``` > [!WARNING] > > `message` is written for developers. It isn't translated and isn't meant for your customers. Turn `code` into your own wording instead of showing `localizedDescription` on screen. 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:)`: | Status | Code | Description | | - | - | - | | 400 | `VALIDATION_INVALID_BODY` | The 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. | | 400 | `VALIDATION_INVALID_JSON` | The request body wasn't valid JSON. | | 400 | `VALIDATION_MISSING_USER_IDENTIFIER` | The device ID header was missing. You shouldn't see this; the SDK always sends it. | | 400 | `AUTH_INVALID_USER_AGENT` | The SDK's `User-Agent` header was missing or changed. You should only see this if something rewrites your headers. | | 400 | `CHAT_RETRY_NO_USER_MESSAGE` | The message you passed to `retry` has no user message before it to answer. | | 401 | `AUTH_INVALID_JWT` | The 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](/docs/ios-sdk/user-identity). | | 402 | `CHAT_CREDITS_EXHAUSTED` | The workspace has no message credits left. Upgrade the plan, add credits, or wait for the monthly reset. | | 403 | `AUTH_OWNERSHIP_MISMATCH` | The conversation belongs to a different user or device, or doesn't exist. | | 403 | `CHAT_CONVERSATION_MISMATCH` | The conversation belongs to a different agent. | | 403 | `CHAT_CONVERSATION_NOT_ONGOING` | The 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. | | 403 | `ACCESS_COUNTRY_BLOCKED` | The agent blocks requests from the device's country. | | 404 | `AGENT_NOT_FOUND` | No agent has that ID, or the **iOS SDK** channel's **Visibility** is off. See [Quick Start](/docs/ios-sdk/quickstart). | | 404 | `RESOURCE_NOT_FOUND` | The address the SDK called doesn't exist. | | 404 | `CHAT_RETRY_MESSAGE_NOT_FOUND` | The message ID you gave `retry` isn't an agent message in that conversation. | | 404 | `RESOURCE_TOOL_CALL_NOT_FOUND` | The tool call wasn't found, or it expired. This can come up during the tool loop. | | 404 | `RESOURCE_TOOL_CALL_MISMATCH` | The tool call belongs to a different conversation. | | 404 | `RESOURCE_TOOL_RESULT_NOT_PENDING` | The server wasn't waiting for this tool result. Usually it was sent twice. | | 429 | `RATE_LIMIT_TOO_MANY_REQUESTS` | Too many requests: the limit is 1,000 every 10 seconds per device and agent. Wait and try again. | | 500 | `CHAT_STREAMING_ERROR` | The answer failed on the server while streaming. Safe to try again. | | 500 | `INTERNAL_SERVER_ERROR` | Something went wrong on the server. Try again, or contact support if it keeps happening. | > [!NOTE] > > `AGENT_NOT_FOUND` looks the same whether the agent doesn't exist or the channel is off. If you're sure the agent ID is right, open **Channels** → **iOS SDK** → **Overview** and check **Visibility** first. ## Handling errors > [tab] async/await > > ```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.") > } > ``` > [tab] Network problems > > Connection problems come back as `.network`, with the system's description of the failure: > > ```swift > catch QwryError.network(let description) { > logger.info("SDK request failed: \(description)") > show("Couldn't reach the assistant. Check your connection and try again.") > } > ``` > > Change timeouts with the `URLSessionConfiguration` you pass to `QwryClient(agentId:configuration:)`. > [tab] SwiftUI state > > `ConversationState` and `ConversationListState` never throw. The failure goes into `error`: > > ```swift > if let error = chat.error { > Text(userFacingMessage(for: error)) > .foregroundStyle(.red) > } > ``` > > A failed `send` also removes the empty agent bubble, so the list only shows messages that exist. See [SwiftUI](/docs/ios-sdk/swiftui). > [tab] Rate limits > > Wait before trying again, and make each wait longer than the last: > > ```swift > func withRetry( > attempts: Int = 3, > _ operation: () async throws -> T > ) async throws -> T { > var delay: Duration = .milliseconds(500) > var attempt = 1 > while true { > do { > return try await operation() > } catch let error as QwryError where error.isRateLimited && attempt < attempts { > try await Task.sleep(for: delay + .milliseconds(.random(in: 0...200))) > delay *= 2 > attempt += 1 > } > } > } > ``` > > The small random amount keeps many devices from retrying at the same moment. > > > [!NOTE] > > > > Tool results are already retried inside the SDK, 3 times with a growing wait. Don't add your own retry around tool handlers. ## 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](/docs/ios-sdk/client-side-tools). ## 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. > [!WARNING] > > Don't send identity tokens or message text to a crash-reporting service. `QwryError.localizedDescription` holds only the server's message, which is safe to log. ## Related > [card] Streaming | /docs/ios-sdk/streaming | Where most errors show up. > [card] User Identity | /docs/ios-sdk/user-identity | Handling expired and rejected tokens. > [card] Client-Side Tools | /docs/ios-sdk/client-side-tools | Reporting failures back to the agent. > [card] Overview | /docs/ios-sdk/overview | Requirements and rate limits.