Roman Vasilyev
01/06/2021, 5:52 AMexpectSuccess
stopped working, 1.4.3 triggered validateResponse
on 404 e.t.c, now not:
fun HttpClientConfig<*>.myInit() {
expectSuccess = false
addMyValidator()
}
fun HttpClientConfig<*>.addMyValidator() {
HttpResponseValidator {
validateResponse { response ->
}
}
}
Rustam Siniukov
01/06/2021, 12:23 PMexpectSuccess
to false
? Validation should be executed only if you set expectSuccess=true
and previous behaviour was wrongRoman Vasilyev
01/06/2021, 4:18 PMRustam Siniukov
01/06/2021, 4:20 PMRoman Vasilyev
01/06/2021, 4:23 PMfun HttpClientConfig<*>.addMyValidator() {
HttpResponseValidator {
validateResponse { response ->
val statusCode = response.status.value
val originCall = response.call
if (statusCode < 300) return@validateResponse
val exceptionResponse = originCall.response
val exceptionResponseText = exceptionResponse.readText()
when (statusCode) {
in 300..399 -> throw RedirectResponseException(exceptionResponse, exceptionResponseText)
403 -> {
val bytes = exceptionResponse.readBytes().decodeToString()
val err = nonStrict.decodeFromString(ErrorResponse.serializer(), bytes)
throw LoginRequiredException(err, exceptionResponse)
}
in 400..402 -> {
val bytes = exceptionResponse.readBytes().decodeToString()
val err = nonStrict.decodeFromString(ErrorResponse.serializer(), bytes)
throw ErrorResponseException(err, exceptionResponse)
}
404 -> {
throw ErrorResponseException(ErrorResponse(detail = "Not found"), exceptionResponse)
}
in 405..499 -> throw ClientRequestException(exceptionResponse, exceptionResponseText)
in 500..599 -> throw ServerResponseException(exceptionResponse, exceptionResponseText)
else -> throw ResponseException(exceptionResponse, exceptionResponseText)
}
}
}
}
Rustam Siniukov
01/06/2021, 4:36 PMexpectSuccess
to true
, but add more logic to HttpResponseValidator
by adding CallExceptionHandler
. Something like this
fun HttpClientConfig<*>.myInit() {
expectSuccess = true
addMyValidator()
}
fun HttpClientConfig<*>.addMyValidator() {
HttpResponseValidator {
processException { responseException ->
if (responseException !is ClientRequestException) return@processException
when (responseException.response.status) {
...
}
}
}
}
This way you can extend the default behaviour without copy-pasting framework codeRoman Vasilyev
01/06/2021, 4:51 PMfun HttpClientConfig<*>.addMyValidator() {
HttpResponseValidator {
handleResponseException { responseException ->
if (responseException !is ClientRequestException) return@handleResponseException
val exceptionResponse = responseException.response
when (exceptionResponse.status) {
HttpStatusCode.Forbidden -> { // 403
val exceptionResponseText = exceptionResponse.readText()
val err = nonStrict.decodeFromString(ErrorResponse.serializer(), exceptionResponseText)
throw LoginRequiredException(err, exceptionResponse)
}
HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized -> { // in 400, 401
val exceptionResponseText = exceptionResponse.readText()
val err = nonStrict.decodeFromString(ErrorResponse.serializer(), exceptionResponseText)
throw ErrorResponseException(err, exceptionResponse)
}
HttpStatusCode.NotFound -> { // 404
throw ErrorResponseException(ErrorResponse(detail = "Not found"), exceptionResponse)
}
}
}
}
}
csieflyman
01/08/2021, 1:40 AMRoman Vasilyev
01/08/2021, 1:55 AMexpectSuccess = true
should trigger validateResponse
only for statusCode < 299
csieflyman
01/08/2021, 2:13 AMRoman Vasilyev
01/08/2021, 2:15 AM