Last active
March 3, 2022 07:01
-
-
Save cp-hardik-p/2cbab0d9f63e3ecd885cd2623633c66a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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