I have this function that makes a network request and writes results to SQLDelight database:
@Throws(Exception::class)
suspend fun updateData()
In iOS project in Xcode I see that this function is translated into a function with completionHandler of type (KotlinUnit?, Error?) -> Void
.
In my case Error type has only 1 visible property - localizedDescription
. If I cast to NSError
then I can get more visible fields but none of them are of any help.
I saw in logs that every error in Ktor
http client is thrown as Error
in Swift. This makes it hard to know what kind of error was thrown.
I want to be able to see the error type and react differently for certain cases. For example, if it was network unavailable error, to show my custom error text with translations, or if it was 401 error send user to auth page, 500 show some other text, etc.
For Android app I guess I can just forward those exceptions and still be able to type check or access more data about error. But I don't know what to do for iOS app. The solution ideally should work great on both platforms.
What are the best practices for handling network errors in iOS and in Android or in general any error/exception that are thrown from shared module in multiplatform project?
With Kotlin Multiplatform, you can create different multiplatform projects for multiple platforms, including web, desktop, and other native platforms. Kotlin applications will work on different operating systems, such as macOS, Windows, Linux, Android, iOS, watchOS, and others.
Stable and customizable integration with iOS With the KMM Plugin you can run, test, and debug the iOS part of your application on iOS targets straight from Android Studio. Since the first release of the plugin, its iOS integration has become much more stable and configurable, and it now supports the latest iOS tooling.
Kotlin Multiplatform Mobile allows you to use a single codebase for the business logic of iOS and Android apps. So if you develop a very thin client and the majority of the code is UI logic, then the main power of Kotlin Multiplatform Mobile will be unused in your project.
You can use a MVP
architecture and let the Presenter
component deal with these errors and it just indicates to the view where to go.
Something like that:
shared/MyPresenter.kt
// ...
when (throwable) {
is Unauthorized -> {
view?.showInvalidCredentials()
}
is Conflict -> {
view?.showConflictAccount()
}
else -> {
view?.showError(throwable)
}
}
With this approach you'll remove that responsibility from the platform Views.
A good project to take as a guide is that: https://github.com/jarroyoesp/KotlinMultiPlatform
You can map responses in your network layer to a Result
. Often the data modelling of result is specific to your application, so can include some combination of loading/error/result by using generics or sealed classes.
You can also use the Result type in the kotlin standard library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With