ivano
01/21/2020, 2:21 PMcompanion object {
fun <T> fromData(data: T): Result<T> {
return Result(data, null)
}
fun <T> fromError(error: Throwable): Result<T> {
return when (error) {
is HttpException -> {
if (error.code() in 400..499) {
if (error.code() == 401) AccountStorage().signOut()
mapErrorBody(error, PAErrorModel::class.java)?.let {
return Result(null, it)
} ?: return Result(null, PAErrorModel("", error.localizedMessage))
}
return Result(null, PAErrorModel("", error.localizedMessage))
}
else -> Result(null, error)
}
}
}ivano
01/21/2020, 2:53 PMivano
01/21/2020, 2:53 PMclass Result<T>(val data: T? = null, val error: Throwable? = null) {
companion object {
fun <T> fromData(data: T): Result<T> {
return Result(data, null)
}
fun <T> fromError(error: Throwable): Result<T> {
return if (error is HttpException) {
manageCodeHttpException(error)
} else Result(null, error)
}
private fun <T> manageCodeHttpException(error: HttpException): Result<T> {
return checkRange(error)
}
private fun <T> checkRange(error: HttpException): Result<T> {
if (error.code() in 400..499) {
if (error.code() == 401) AccountStorage().signOut()
mapErrorBody(error, PAErrorModel::class.java)?.let {
return@let Result(null, it)
} ?: return Result(null, PAErrorModel("", error.localizedMessage))
}
return Result(null, PAErrorModel("", error.localizedMessage))
}
}Mike
01/21/2020, 3:06 PMreturn statements in the is HttpException, and add an else to the if(error.code() in 400..499) to wrap the Result(null, PAErrorModel("", error.localizedMessage) return value. This will keep it all in one function.Mike
01/21/2020, 3:08 PMResult with the first element null anyway.Luis Munoz
01/23/2020, 4:40 PMLuis Munoz
01/23/2020, 4:41 PMLuis Munoz
01/23/2020, 4:41 PMMike
01/23/2020, 6:04 PMreturn statements in your functions.ivano
01/27/2020, 9:42 AM