Skip to content

Instantly share code, notes, and snippets.

@navi25
Last active March 25, 2020 07:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save navi25/67176730f5595b3f1fb5095062a92f15 to your computer and use it in GitHub Desktop.
Save navi25/67176730f5595b3f1fb5095062a92f15 to your computer and use it in GitHub Desktop.
open class BaseRepository{
suspend fun <T : Any> safeApiCall(call: suspend () -> Response<T>, errorMessage: String): T? {
val result : Result<T> = safeApiResult(call,errorMessage)
var data : T? = null
when(result) {
is Result.Success ->
data = result.data
is Result.Error -> {
Log.d("1.DataRepository", "$errorMessage & Exception - ${result.exception}")
}
}
return data
}
private suspend fun <T: Any> safeApiResult(call: suspend ()-> Response<T>, errorMessage: String) : Result<T>{
val response = call.invoke()
if(response.isSuccessful) return Result.Success(response.body()!!)
return Result.Error(IOException("Error Occurred during getting safe Api result, Custom ERROR - $errorMessage"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment