Android SDK
Error Handling
The one exception the QwryAI Android SDK throws, its codes and how to handle them
One exception type
Every failure the SDK reports is a QwryException. Its code tells you what went wrong, and its status tells you whether the server refused the request or the SDK gave up on its own:
QwryException
├── status != null the server answered with an error (HTTP status)
└── status == null the SDK's own codes: NETWORK_ERROR, INVALID_RESPONSE, TOOL_LOOP_LIMIT_EXCEEDEDQwryException
class QwryException : Exception, Package: com.qwryai.sdk
class QwryException(
val status: Int?,
val code: String,
message: String,
val details: Map<String, String>? = null
) : Exception(message)status Int?
The HTTP status of the server's error response, such as 401, 403 or 429. null for the SDK's own codes.
code String required
A stable, machine-readable code. Use it in your logic.
message String required
A readable description, from the server or the SDK.
details Map<String, String>?
Field-by-field reasons when a request failed validation, such as cursor → Invalid cursor.
Convenience properties
val isRateLimited: Boolean // status == 429
val isNotFound: Boolean // status == 404
val isCreditsExhausted: Boolean // status == 402The SDK's own codes
QwryException.NETWORK_ERROR // The request never got an answer
QwryException.INVALID_RESPONSE // The server sent something the SDK could not read
QwryException.TOOL_LOOP_LIMIT_EXCEEDED // The agent called tools more than maxToolLoopSteps times in a rowError codes
These are the codes you can meet in QwryException.code:
| Status | Code | When |
|---|---|---|
| 400 | VALIDATION_INVALID_BODY | The request failed validation; details names the field. Also returned for a tool result over 20 KB and for a cursor the server didn't issue. |
| 400 | CHAT_RETRY_NO_USER_MESSAGE | The message passed to retry() has no user message before it. |
| 401 | AUTH_INVALID_JWT | The token passed to identify(), or the one stored from it, is invalid or expired, or the agent has no identity verification secret. See User Identity. |
| 402 | CHAT_CREDITS_EXHAUSTED | The workspace has no message credits left. Caught by isCreditsExhausted. |
| 403 | AUTH_OWNERSHIP_MISMATCH | The conversation belongs to another device or user, or doesn't exist. Also returned by retry() and listMessages() for an unknown conversation. |
| 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 another answer or client-side action is still in progress in it. Start a new conversation, or wait and try again. |
| 403 | ACCESS_COUNTRY_BLOCKED | The agent blocks requests from the user's country. |
| 404 | AGENT_NOT_FOUND | No agent has this ID, or the Android SDK channel's Visibility is off. See Quick start. Caught by isNotFound. |
| 404 | CHAT_RETRY_MESSAGE_NOT_FOUND | The message passed to retry() isn't in the conversation. Caught by isNotFound. |
| 404 | RESOURCE_TOOL_CALL_NOT_FOUND | The tool call wasn't found or has expired. Can surface from the automatic tool loop. Caught by isNotFound. |
| 404 | RESOURCE_TOOL_CALL_MISMATCH | The tool call belongs to another conversation. Caught by isNotFound. |
| 404 | RESOURCE_TOOL_RESULT_NOT_PENDING | The tool's result was already sent. Caught by isNotFound. |
| 429 | RATE_LIMIT_TOO_MANY_REQUESTS | More than 1,000 requests in 10 seconds from this install. Wait and try again. Caught by isRateLimited. |
| 500 | CHAT_STREAMING_ERROR | The answer failed while it was streaming. Safe to retry. |
| 500 | INTERNAL_SERVER_ERROR | Something unexpected went wrong on the server. Try again, and contact us if it keeps happening. |
| none | NETWORK_ERROR | No answer: no connection, a DNS failure or a timeout. |
| none | INVALID_RESPONSE | The server's response couldn't be read. |
| none | TOOL_LOOP_LIMIT_EXCEEDED | The agent kept calling tools past maxToolLoopSteps (10 by default). |
Handling errors
Wrap calls in try/catch:
try {
val response = client.send("Hello")
} catch (e: QwryException) {
when {
e.code == QwryException.NETWORK_ERROR -> {
println("No connection. Check the network and try again.")
}
e.status == 401 -> {
println("Sign-in expired. Get a new token from your server.")
}
e.status == 403 -> {
println("Access denied: ${e.code}")
}
e.isRateLimited -> {
println("Too many requests. Wait and retry.")
}
e.isCreditsExhausted -> {
println("No message credits left.")
}
e.isNotFound -> {
println("Not found. Check the agent ID and the channel's Visibility.")
}
else -> {
println("Error: ${e.code}: ${e.message}")
}
}
}ConversationState and ConversationListState catch the exception for you and keep it in error, a StateFlow that the next call clears:
val chat = ConversationState(client)
lifecycleScope.launch {
chat.error.collect { error ->
if (error != null) showBanner(error.message ?: error.code)
}
}
lifecycleScope.launch { chat.send("Hello") }