Pablo
01/02/2022, 12:59 PMsealed class OrganizationExceptions : Exception() {
object OrganizationNotFound : OrganizationExceptions()
object ListNotAvailable : OrganizationExceptions()
}
This is an example, but it won't be always "OrganizationExceptions" I'm creating a method generic, also I don't know if it's better to create a sealed class or create
class OrganizationNotFoundException : Throwable() //or Exception()
class ListNotAvailable : Throwable() //or Exception()
Any recomendations?
the method signature is :
fun apiCallWithStatusCode(codes : HashMap<Int, Throwable>, apiCall : suspend () -> Response<T>,){...}
Arjan van Wieringen
01/02/2022, 6:00 PMval codes = mapOf(
400 -> OrganizationNotFound,
500 -> CustomException(someData)
);
Arjan van Wieringen
01/02/2022, 6:00 PMPablo
01/02/2022, 6:42 PMJames Whitehead
01/02/2022, 8:26 PMfun <T : Exception> apiCallWithStatusCode(
codes: HashMap<Int, T>,
apiCall : suspend () -> Response<HashMap<Int, T>>
) {
...
}
Michael de Kaste
01/03/2022, 12:32 PMThrowable
of your method signature to an out Throwable
, maybe something like this?
interface ExceptionCodeMap {
val codes: Map<Int, Throwable>
}
sealed class OrganizationExceptions : Exception(){
object OrganizationNotFound : OrganizationExceptions()
object ListNotAvailable : OrganizationExceptions()
companion object : ExceptionCodeMap {
override val codes = mapOf(
400 to OrganizationNotFound,
500 to ListNotAvailable
)
}
}
fun main(){
apiCallWithStatusCode(OrganizationExceptions.codes){
...
}
}
Also, maybe dont specify map implementation in the api, otherwise, instead of mapOf
create a custom HashMapMichael de Kaste
01/03/2022, 12:45 PMfun <T : Throwable> apiCallWithStatusCode(codes: Map<Int, T>, apiCall : suspend () -> Response<T>,){...}
you have access to the correct type T within the functionPablo
01/04/2022, 7:14 AMclass MyCustomException : Throwable
Pablo
01/04/2022, 7:15 AMPablo
01/04/2022, 8:08 PM