Skip to content

Instantly share code, notes, and snippets.

@thomaskioko
Created July 25, 2023 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaskioko/a7fd6dc8dc1207f7cf2e9f576b57e998 to your computer and use it in GitHub Desktop.
Save thomaskioko/a7fd6dc8dc1207f7cf2e9f576b57e998 to your computer and use it in GitHub Desktop.
HttpClient(httpClientEngine) {
expectSuccess = true
...
HttpResponseValidator {
validateResponse { response ->
if (!response.status.isSuccess()) {
val httpFailureReason = when (response.status) {
HttpStatusCode.Unauthorized -> "Unauthorized request"
HttpStatusCode.Forbidden -> "${response.status.value} Missing API key"
HttpStatusCode.NotFound -> "Invalid Request"
HttpStatusCode.UpgradeRequired -> "Upgrade to VIP"
HttpStatusCode.RequestTimeout -> "Network Timeout"
in HttpStatusCode.InternalServerError..HttpStatusCode.GatewayTimeout -> "${response.status.value} Server Error"
else -> "Network error!"
}
throw HttpExceptions(
response = response,
cachedResponseText = response.bodyAsText(),
httpFailureReason = httpFailureReason,
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment