Hi I found out that Ktor has a nasty exception thr...
# multiplatform
g
Hi I found out that Ktor has a nasty exception thrown when connection to the host cannot be established. For android it's
java.net.ConnectException
As far as I understood from the several issue descriptions in various sources, this exception is uncatchable from commonMain. I'm trying to solve this by introducing expect/actual declarations for the exception, but running into a problem of not being able to make my actual declarations compatible. My current code is something like this: commonMain:
expect class RequestTimedOutException: Exception
androidMain:
actual class RequestTimedOutException: ConnectException()
But the code wouldn't compile, because "_Actual class 'RequestTimedOutException' has no corresponding expected declaration The following declaration is incompatible because some supertypes are missing in the actual declaration_", even though Kotlin's Exception is a
typealias
of
java.lang.Exception
Has anyone dealt with such a problem? How did you solve it? Is there a better solution for the underlying problem of handling the time out situation in commonMain? Thanks Upd: I figured doing some kind of a wrapper with expect/actual that catches the exception and then throws a different one that could be caught is working way to do it, but ideally I want to have something less sketchy
m
A hack I did, was add an expected extension function on
Exception
to see if the exception was something I wanted to handle. I then checked that in the catch block and handled it differently than the other exceptions. This was related to database things and not network, but the concept is the same.
g
Yeah, well if I understood you correctly, that's not gonna help here, because the problem is not just to avoid catching other exceptions, but to be able to catch that exception in the first place, because right now, I can add a catch block like this
catch(e: Throwable)
and the app will still crash
m
Yeah, then my suggestion won't work. Not sure how
catch(e: Throwable)
doesn't catch something on Android. I could see it not working on JS or iOS maybe.
g
Yeah, me neither...
Upon further investigation I found out that it seems I'm unable to catch any exception in coroutines in commonMain. I removed the http request completely and instead try throwing different types of exceptions to catch them some where down the call stack, but they are never caught...
m
I'm going to assume that the exception is being thrown in a different coroutine than the one catching it, and a default exception handling is processing it before you can. Not sure if this is from you doing launches or async or if ktor is doing something. https://kotlinlang.org/docs/exception-handling.html
g
No it is being thrown in exactly the same coroutine with launch. But I managed to solve this problem somehow. Or rather it got solved by itself, I was experimenting with GraphQL and had to rebuild the module to get my queries generated. Once I did that the issue magically dissolved...