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

Fail

07/26/2019, 2:27 PM
Why
Copy code
expectSuccess = false
not working at 1.2.2? On Ktor multiplatform I catch Platform exception I tried the following options:
Copy code
HttpClient {
            expectSuccess = false
and
Copy code
HttpClient {
HttpResponseValidator {
                validateResponse {response ->
                    if (response.status.value!=200) {
                        throw ResponseException(response)
                    }
                }

            }
And
Copy code
ResponseException
not catched. Only
Copy code
Exception
not Solved 😞
e

e5l

07/26/2019, 2:28 PM
Hi @Fail, could you provide the exception?
f

Fail

07/26/2019, 2:41 PM
Connection refused
Copy code
[DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - METHOD: HttpMethod(value=GET)
W/System.err: [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - COMMON HEADERS
    [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - -> Accept: application/json
W/System.err: [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - -> Accept-Charset: UTF-8
    [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - CONTENT HEADERS
    [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - BODY Content-Type: null
    [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - BODY START
W/System.err: [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - BODY END
W/System.err: [DefaultDispatcher-worker-7] INFO io.ktor.client.HttpClient - RESPONSE: 400 Bad Request
    [DefaultDispatcher-worker-7] INFO io.ktor.client.HttpClient - METHOD: HttpMethod(value=GET)
W/System.err: [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - REQUEST: <http://localhost/>
W/System.err: [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - METHOD: HttpMethod(value=GET)
W/System.err: [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - COMMON HEADERS
W/System.err: [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - -> Accept: application/json
    [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - -> Accept-Charset: UTF-8
    [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - CONTENT HEADERS
W/System.err: [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - BODY Content-Type: null
    [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - BODY START
    [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - BODY END
W/System.err: [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - REQUEST <http://localhost/> failed with exception: java.net.ConnectException: Connection refused
my clientCreation:
Copy code
fun getClient(apiSerializer: KotlinxSerializer?): HttpClient {
        return HttpClient {
            expectSuccess = true

            HttpResponseValidator {
                validateResponse { response ->
                    val statusCode = response.status.value
                    when (statusCode) {
                        in 300..399 -> throw RedirectResponseException(response)
                        in 400..499 -> throw ClientRequestException(response)
                        in 500..599 -> throw ServerResponseException(response)
                    }

                    if (statusCode >= 600) {
                        throw ResponseException(response)
                    }
                }
            }

            if (TrendSession.instance.debugNetworkRequest) {
                install(Logging) {
                    logger = Logger.DEFAULT
                    level = LogLevel.ALL
                }
            }

            if (apiSerializer!=null) {
                install(JsonFeature) {
                    serializer = apiSerializer
                }
            }

        }
    }
4 Views