Goku
04/13/2021, 12:25 PM<https://api.github.com/projects/columns/42>
in order to receive 401, which is intended.
Unfortunately, the program crashes with the following explanation:
Exception doesn't match @Throws-specified class list and thus isn't propagated from Kotlin to Objective-C/Swift as NSError.
It is considered unexpected and unhandled instead. Program will be terminated.
Uncaught Kotlin exception: io.ktor.client.features.ClientRequestException: Client request(<https://api.github.com/projects/columns/42>) invalid: 401 Unauthorized. Text: "{"message":"Requires authentication","documentation_url":"<https://docs.github.com/rest/reference/projects#get-a-project-column>"}"
I get back the response and everything gets logged properly.
Any idea on how to fix this?
Thanks!Rustam Siniukov
04/13/2021, 1:28 PMexpectSuccess = false
on client or per-request levelGoku
04/13/2021, 1:57 PMthis.install(HttpSend) {
intercept { httpClientCall, httpRequestBuilder ->
val statusCode = httpClientCall.response.status.value
if (statusCode !in 100..399) {
val delayBetweenRequests = retryStrategy.retryDelay(httpClientCall) ?: 0.0
delay(maxOf(delayBetweenRequests, 0.0).toLong())
execute(httpRequestBuilder)
} else {
httpClientCall
}
}
maxSendCount = retryStrategy.retryCount
}
}
For some reason I keep getting the following exception:
Uncaught Kotlin exception: io.ktor.client.features.SendCountExceedException: Max send count 3 exceeded
But I don’t understand how this is possibleinstall(Auth) {
basic {
username = "myUsername"
password = "myPassword"
// sendWithoutRequest = true
}
}
/**
* Send credentials in without waiting for [HttpStatusCode.Unauthorized].
*/
var sendWithoutRequest: Boolean = false
The request that gets executed returns 401 (Unauthorized) but it doesn’t get re-executed with the Authorization
header…
I can only send it once I set sendWithoutRequest = true
which I really don’t want to do for every request.
Any help would be greatly appreciated!
Thanks!Rustam Siniukov
04/13/2021, 2:37 PMSendCountExceedException
what exactly is wrong? It seems like you send more then 3 retries, and it’s your current limitGoku
04/13/2021, 2:42 PMHttpSend.kt
class:
if (sentCount >= maxSendCount) {
throw SendCountExceedException("Max send count $maxSendCount exceeded")
}
So once it reaches 3 it will throw an exception, so I don’t understand how I can fix this on my side?
All I do is set the maxSendCount = 3
in here:
this.install(HttpSend) {
intercept { httpClientCall, httpRequestBuilder ->
...
}
maxSendCount = 3
}
Rustam Siniukov
04/13/2021, 2:43 PMGoku
04/13/2021, 2:44 PMException doesn't match @Throws-specified class list and thus isn't propagated from Kotlin to Objective-C/Swift as NSError.
It is considered unexpected and unhandled instead. Program will be terminated.
Uncaught Kotlin exception: io.ktor.client.features.SendCountExceedException: Max send count 3 exceeded
Rustam Siniukov
04/13/2021, 2:46 PMexecute
in your interceptor once it reaches desired valuethis.install(HttpSend) {
var couter = 0
intercept { httpClientCall, httpRequestBuilder ->
val statusCode = httpClientCall.response.status.value
if (counter < 3 && statusCode !in 100..399) {
...
execute(httpRequestBuilder)
counter++
} else {
httpClientCall
}
}
}
Goku
04/13/2021, 2:49 PMRustam Siniukov
04/13/2021, 2:54 PMGoku
04/13/2021, 2:56 PMrsetkus
04/14/2021, 1:46 PMHttpSend
to intercept response for authentication purposes. On 401/Unauthorized response intercepting and performing actions to refresh token and retry request. When expectSuccess
is turned on (value true), 401 response is not intercepted. I guess it happens due to ExpectSuccess
feature, which throws exception just before HttpSend
feature and stops proceeding ktor pipeline.
When I turn off expectSuccess
(value false), response is intercepted in HttpSend
and authentication works as expected. However, none of the HttpResponseValidator
functions (validateResponse and handleResponseException) are called.
Can you suggest or what is preferred way to map http errors to custom errors when expectSuccess
is off?Rustam Siniukov
04/14/2021, 1:59 PMexpectSuccess=false
will disable all validators, and not only the default one. It was fixed in 1.5.2 https://youtrack.jetbrains.com/issue/KTOR-2007.
Another point, please consider using Auth
feature and implementing custom AuthProvider
instead of intercepting HttpSend
. Internally it will do the same, but it is more idiomatic way. Not a big deal, thoughrsetkus
04/14/2021, 2:10 PMHttpSend
is marked as experimental so, definitely will consider option re-implement authentication using AuthProvider
.Rustam Siniukov
04/14/2021, 2:11 PMrsetkus
04/14/2021, 4:22 PMHttpSend
doesn’t intercept response anymore.Rustam Siniukov
04/14/2021, 6:31 PMrsetkus
04/14/2021, 6:33 PMRustam Siniukov
04/15/2021, 11:31 AM