retrofit icon indicating copy to clipboard operation
retrofit copied to clipboard

How to get the URL endpoint and the GET/POST method when an Exception occurs?

Open vstung89 opened this issue 1 year ago • 0 comments
trafficstars

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?

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))
	}
}

vstung89 avatar Jun 20 '24 17:06 vstung89