Foso / Ktorfit

HTTP client generator / KSP plugin for Kotlin Multiplatform (Android, iOS, Js, Jvm, Native) using KSP and Ktor clients inspired by Retrofit https://foso.github.io/Ktorfit
https://foso.github.io/Ktorfit
Apache License 2.0
1.55k stars 40 forks source link

Catching Fatal Errors #412

Open ferenczAndras opened 1 year ago

ferenczAndras commented 1 year ago

Hi! Is there a way, to set up an error handler, whenever a fatal error happens, the app does not stop, but it gets caught?

For example, when there is no internet connection for the app, I am getting errors like:

FATAL EXCEPTION: main
 Process: com.app.app, PID: 5056
  java.nio.channels.UnresolvedAddressException
 at sun.nio.ch.Net.checkAddress(Net.java:107)
at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:663)
at io.ktor.network.sockets.SocketImpl.connect$ktor_network(SocketImpl.kt:44)
at io.ktor.network.sockets.ConnectUtilsJvmKt.connect(ConnectUtilsJvm.kt:21) 
at io.ktor.network.sockets.TcpSocketBuilder.connect(TcpSocketBuilder.kt:37)
at io.ktor.client.engine.cio.ConnectionFactory.connect(ConnectionFactory.kt:30)
ferenczAndras commented 1 year ago

Hi, @Foso can you help me point to somewhere, where I can catch the Fatal Errors thrown if there is no internet on the device? What is the best practice for this?

Foso commented 1 year ago

Hi @ferenczAndras , currently your only way to detect that exception is to wrap the code, where you do the request, inside a try-catch. I started working on improving that situation https://github.com/Foso/Ktorfit/issues/389