Hey Guys, I am working in ktor api response. I wan...
# multiplatform
v
Hey Guys, I am working in ktor api response. I want to handle api response. So i read from some medium post. I tried some code. Can you suggest me if there is any better approach then this. Is this code will work in both platform?
Copy code
package com.example.kotlinmultiplatformsharedmodule

sealed class ApiResponse<out T : Any> {

    data class Success<out T : Any>(
        val data: T?
    ) : ApiResponse<T>()

    data class Error(
        val exception: Throwable? = null,
        val responseCode: Int = -1,
        val errorResponse: ErrorResponse? = null
    ) : ApiResponse<Nothing>()

    fun handleResult(onSuccess: ((responseData: T?) -> Unit)?,onError: ((error: Error) -> Unit)?) {
        when (this) {
            is Success -> {
                onSuccess?.invoke(this.data)
            }
            is Error -> {
                onError?.invoke(this)
            }
        }
    }
}

data class ErrorResponse(
    val errorCode: Int,
    val errorMessage: String
)