ktor: Ktor client cannot catch offline exceptions (Kotlin Multiplatform)

Ktor Version

1.2.0

Ktor Engine Used(client or server and name)

IOS Client

JVM Version, Operating System and Relevant Context

iOS - Airplane mode

Feedback

Tried to catch offline mode exception in case the internet connection is dropped. The errors showed up in the logs but cannot be caught in Multiplatform common project. Haven’t tried on Android yet.

2019-06-03 16:02:17.432609+0800 XApp[1558:1117673] Task <449DDD76-DA3D-42B4-9CA4-ECEA0B218850>.<1> load failed with error Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x282e59d40 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <449DDD76-DA3D-42B4-9CA4-ECEA0B218850>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <449DDD76-DA3D-42B4-9CA4-ECEA0B218850>.<1>"
), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://localhost/?page=1&ordering=modified&search=, NSErrorFailingURLKey=https://localhost/?page=1&ordering=modified&search=, _kCFStreamErrorDomainKey=1} [-1009]

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 14
  • Comments: 20 (2 by maintainers)

Commits related to this issue

Most upvoted comments

@e5l when it’s going to be released the fix?

Same issue here with android. I get a “java.net.UnknownHostException” when there’s no internet connection (eg: airplane mode) which is of course only available on jvm, so I can’t catch it on the common side. I think it should be possible to handle the exception on the platform specific side, but I’d like to know if that’s just a “temporary workaround” or it is the desired behaviour

I have the same problem, @LiewJunTung how did you catch this exception?

In Android is working fine, but in IOS crashes, seems a problem of IosClientEngine and the delegate protocol.

I had the same problem. But I’ve solved it by catching a Throwable instead of an Exception in the try/catch block.

Hope it helps.

Same issue here with android. I get a “java.net.UnknownHostException” when there’s no internet connection (eg: airplane mode) which is of course only available on jvm, so I can’t catch it on the common side. I think it should be possible to handle the exception on the platform specific side, but I’d like to know if that’s just a “temporary workaround” or it is the desired behaviour

And if you are using CIO on Android you get an java.nio.channels.UnresolvedAddressException which again you can’t catch in common code, is there a solution for this part of the issue yet?

i think reason of error is this line - https://github.com/ktorio/ktor/blob/63c508918543774b857977ad033d602e04491dc4/ktor-client/ktor-client-ios/darwin/src/io/ktor/client/engine/ios/IosUtils.kt#L40 IosHttpRequestException not Exception but just Throwable. it’s reason why we not catch this Exception when use catch(e: Exception) and why catch(e: Throwable) will help. i think this case should be Exception, not just Throwable.

This only happens in release mode. Also true for you guys?

@tonilopezmr for now I’m not handling it. And will wait for the fix by @e5l. 😃