KHttpException

sealed class KHttpException(message: String) : Exception(source)

Sealed hierarchy of HTTP-related exceptions thrown by buildKHttpClient.

All subclasses extend Exception and carry a human-readable message parsed from the response body (fields message or error), falling back to the raw body (truncated to 200 chars).

Typical handling:

try {
api.getProducts()
} catch (e: KHttpException.ClientError) {
when (e.statusCode) {
401 -> navigateToLogin()
403 -> showPermissionDenied()
else -> toast("Client error ${e.statusCode}: ${e.message}")
}
} catch (e: KHttpException.ServerError) {
toast("Server error ${e.statusCode}: ${e.message}")
} catch (e: KHttpException.NotAccepted) {
log("Unexpected status: ${e.message}")
}

Inheritors

Constructors

Link copied to clipboard
protected constructor(message: String)

Types

Link copied to clipboard
class ClientError(val statusCode: Int, message: String) : KHttpException

Thrown on 4xx responses.

Link copied to clipboard

Thrown by io.ktor.client.statement.HttpResponse.accept when the response status does not match any of the expected io.ktor.http.HttpStatusCodes.

Link copied to clipboard
class ServerError(val statusCode: Int, message: String) : KHttpException

Thrown on 5xx responses, or with statusCode = -1 for network-level failures (timeout, no connectivity, etc.).

Properties

Link copied to clipboard
open val cause: Throwable?
Link copied to clipboard
open val message: String?