https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
r

rrva

05/09/2022, 11:14 AM
when modeling a library API for multiplatform, what are common patterns for error handling?
Copy code
open class FooLogin(private val clientId: String, private val storage: Storage, private val httpClient: HttpClient) {
    suspend fun startLogin(email: String): Result<LoginSession, StartLoginError> 
}

sealed class Result<out T, out E> {
    data class Success<T, E>(val data: T) : Result<T, E>()
    data class Error<T, E>(val exception: E) : Result<T, E>()

    val ok get() = (this as? Success)?.data
    val err get() = (this as? Error)?.exception
}

enum class StartLoginError {
    INVALID_EMAIL_FORMAT, INVALID_REQUEST, NETWORK_ERROR, DECODING_ERROR, UNKNOWN_ERROR;
}