Error handling in the Android SDK

Handle OctoError in the Kotlin SDK: the error types, 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 Android SDK is an OctoError. It is a sealed class that extends Exception, so you can catch it in a try block around a suspend function or in the catch operator of a Flow. Match on the subclass to decide what to show the customer.

The OctoError type

sealed class OctoError(message: String, val requestId: String? = null) : Exception(message) {
    class InvalidRequest(message: String, requestId: String?) : OctoError(message, requestId)
    class Unauthorized(message: String, requestId: String?) : OctoError(message, requestId)
    class Forbidden(message: String, requestId: String?) : OctoError(message, requestId)
    class NotFound(message: String, requestId: String?) : OctoError(message, requestId)
    class Conflict(message: String, requestId: String?) : OctoError(message, requestId)
    class RateLimited(message: String, requestId: String?, val retryAfterSeconds: Long?) : OctoError(message, requestId)
    class Server(message: String, requestId: String?) : OctoError(message, requestId)
    class Network(message: String) : OctoError(message)
}
Subclass 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. retryAfterSeconds comes from the Retry-After header.
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

try {
    val response = octo.chat(text = input)
    show(response.message.content)
} catch (e: OctoError.RateLimited) {
    delay((e.retryAfterSeconds ?: 5) * 1000)
    retry()
} catch (e: OctoError.Network) {
    showBanner("You seem to be offline.")
} catch (e: OctoError) {
    showBanner("Something went wrong. Reference: ${e.requestId}")
}

Catch the specific subclasses first and OctoError last. Do not catch Exception broadly around suspend functions, because that also swallows CancellationException and stops the coroutine from cancelling.

Retry rules

  • Retry Network, Server and RateLimited. Wait for retryAfterSeconds 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 Flow throws the OctoError. After the stream has started, it emits a StreamEvent.Error. 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.