You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use a suspend function to call the API as shown in the code below. How can I get the URL endpoint and the GET/POST method when an Exception occurs?
suspend fun <T : BaseResponse> safeCallApi(
api: suspend () -> Response<T>
): Result<T> {
return try {
val response = api()
if (response.isSuccessful) {
val body = response.body()
if (body != null) {
Result.success(body)
} else {
Result.failure(BodyNullException())
}
} else {
Result.failure(ServerException())
}
} catch (e: Exception) {
// I want to obtain the information about the URL endpoint and the GET/POST method there.
Result.failure(UnknownException(e))
}
}
The text was updated successfully, but these errors were encountered:
val call:Call<T> = api()
try {
val response:Response<T> = call.awaitResponse()
..
} catch (e:Exception) {
call.request().method // use me
call.request().url // use meResult.failure(UnknownException(e))
}
Yeah there's basically no way to do this when using a "raw" suspend function because we become at the mercy of underlying libraries and how they throw exceptions. An HttpException includes a Response from which you can get the Request, but IOException subtypes won't have that. The only way to retrieve that information is as posted above, where you create the Call explicitly and perform suspended-execution separately.
Hello everyone, I need help with an issue.
I use a suspend function to call the API as shown in the code below. How can I get the URL endpoint and the GET/POST method when an Exception occurs?
The text was updated successfully, but these errors were encountered: