https://kotlinlang.org logo
#ktor
Title
# ktor
r

Roman Vasilyev

01/06/2021, 5:52 AM
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

Rustam Siniukov

01/06/2021, 12:23 PM
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

Roman Vasilyev

01/06/2021, 4:18 PM
@Rustam Siniukov got it, what is the right way to check responses?
r

Rustam Siniukov

01/06/2021, 4:20 PM
can you show your use case?
r

Roman Vasilyev

01/06/2021, 4:23 PM
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

Rustam Siniukov

01/06/2021, 4:36 PM
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

Roman Vasilyev

01/06/2021, 4:51 PM
@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

csieflyman

01/08/2021, 1:40 AM
@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

Roman Vasilyev

01/08/2021, 1:55 AM
@csieflyman according to Rustam
expectSuccess = true
should trigger
validateResponse
only for
statusCode < 299
c

csieflyman

01/08/2021, 2:13 AM
@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

Roman Vasilyev

01/08/2021, 2:15 AM
@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
2 Views