Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Multiplatform Mobile: How to handle errors (network, db, etc.) in iOS?

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?

like image 593
Marat Avatar asked Dec 29 '20 21:12

Marat


People also ask

How does Kotlin multiplatform work on iOS?

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.

Is Kotlin multiplatform mobile stable?

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.

How does Kotlin multiplatform mobile work?

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.


2 Answers

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

like image 106
Jean Carlos Avatar answered Nov 15 '22 06:11

Jean Carlos


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.

like image 35
enyciaa Avatar answered Nov 15 '22 06:11

enyciaa