in ktor 1.5 `expectSuccess` stopped working, 1.4....
# ktor
r
in ktor 1.5
expectSuccess
stopped working, 1.4.3 triggered
validateResponse
on 404 e.t.c, now not:
Copy code
fun HttpClientConfig<*>.myInit() {
    expectSuccess = false
    addMyValidator()
}

fun HttpClientConfig<*>.addMyValidator() {
    HttpResponseValidator {
        validateResponse { response ->
        }
    }
}
r
can you elaborate why you expect validation to be triggered if you set
expectSuccess
to
false
? Validation should be executed only if you set
expectSuccess=true
and previous behaviour was wrong
r
@Rustam Siniukov got it, what is the right way to check responses?
r
can you show your use case?
r
Copy code
fun 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 I need this logic triggered for every request
r
Ok, so if I got it correct, you need to add more exceptions to the default validation. My suggestion would be to use default validation by setting
expectSuccess
to
true
, but add more logic to
HttpResponseValidator
by adding
CallExceptionHandler
. Something like this
Copy code
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 code
r
@Rustam Siniukov good approach, will try it today
@Rustam Siniukov after changes my validator looks like this and works with ` ktor 1.5.0`:
Copy code
fun 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)
                }
            }
        }
    }
}
c
@Rustam Siniukov I set expectSuccess = true and add validateResponse block to validate response body, but ktor execute ktor default validateResponse block first and then throw exception. Maybe use handleResponseException is a workaround solution for me?
r
@csieflyman according to Rustam
expectSuccess = true
should trigger
validateResponse
only for
statusCode < 299
c
@Roman Vasilyev I got it. I misunderstand ktor execute my validateResponse block first. Ktor document -> This feature could be configured multiple times; all validators and handlers are saved and called in order of install.
r
@csieflyman I just followed Rustams recommendation and happy 🙂 I don’t is it expected behavior or not, also it added more purity to my code as well