phldavies
09/04/2025, 5:42 PM404
response as null
without either losing the default response validation (i.e expectSuccess = false
) or intercepting ClientRequestException
?Aleksei Tirman [JB]
09/05/2025, 7:59 AMnull
when calling the body
method with a nullable type?phldavies
09/05/2025, 8:31 AMAleksei Tirman [JB]
09/05/2025, 8:51 AMval client = HttpClient(CIO) {
HttpResponseValidator {
validateResponse { response ->
val statusCode = response.status.value
val originCall = response.call
if (statusCode == HttpStatusCode.NotFound.value) { // Skip 404
return@validateResponse
}
val exceptionCall = if (!originCall.response.isSaved) {
originCall.save()
} else {
originCall
}
val exceptionResponse = exceptionCall.response
val exceptionResponseText = try {
exceptionResponse.bodyAsText()
} catch (_: MalformedInputException) {
"<body failed decoding>"
}
val exception = when (statusCode) {
in 300..399 -> RedirectResponseException(exceptionResponse, exceptionResponseText)
in 400..499 -> ClientRequestException(exceptionResponse, exceptionResponseText)
in 500..599 -> ServerResponseException(exceptionResponse, exceptionResponseText)
else -> ResponseException(exceptionResponse, exceptionResponseText)
}
throw exception
}
}
}
phldavies
09/05/2025, 9:27 AM.requireSuccess()
extension rather than the plugin along with a .onStatus
extension) allowing for this kind of usage:
return get("api/config/id") { url.appendPathSegments("$configId") }
.onStatus(NotFound) { return null }
.requireSuccess()
.body<T>()