Skip to content

Instantly share code, notes, and snippets.

@cp-hardik-p
Last active March 3, 2022 07:01
Show Gist options
  • Save cp-hardik-p/2cbab0d9f63e3ecd885cd2623633c66a to your computer and use it in GitHub Desktop.
Save cp-hardik-p/2cbab0d9f63e3ecd885cd2623633c66a to your computer and use it in GitHub Desktop.
override fun enqueue(callback: Callback<Result<T>>) {
delegate.enqueue(
object : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {
if (response.isSuccessful) {
callback.onResponse(
this@ResultCall,
Response.success(
response.code(),
Result.success(response.body()!!)
)
)
} else {
callback.onResponse(
this@ResultCall,
Response.success(
Result.failure(
HttpException(response)
)
)
)
}
}
override fun onFailure(call: Call<T>, t: Throwable) {
val errorMessage = when (t) {
is IOException -> "No internet connection"
is HttpException -> "Something went wrong!"
else -> t.localizedMessage
}
callback.onResponse(
this@ResultCall,
Response.success(Result.failure(RuntimeException(errorMessage, t)))
)
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment