Skip to content

Instantly share code, notes, and snippets.

@0xZhangKe
Created June 26, 2022 14:33
Show Gist options
  • Select an option

  • Save 0xZhangKe/015fe07dc3dbe30f5c59049b24bc3e52 to your computer and use it in GitHub Desktop.

Select an option

Save 0xZhangKe/015fe07dc3dbe30f5c59049b24bc3e52 to your computer and use it in GitHub Desktop.
override fun enqueue(callback: Callback<Response<S, E>>) {
return delegate.enqueue(object : Callback<S> {
override fun onResponse(call: Call<S>, response: retrofit2.Response<S>) {
if (response.isSuccessful) {
val body = response.body()
responseInstance.success = true
responseInstance.successData = body
} else {
val error = response.errorBody()
val errorBody = when {
error == null -> null
error.contentLength() == 0L -> null
else -> {
try {
errorConverter.convert(error)
} catch (e: Exception) {
null
}
}
}
responseInstance.success = false
responseInstance.errorData = ErrorResponse.ApiError(errorBody, response.code())
}
callback.onResponse(this@ResponseCall, retrofit2.Response.success(responseInstance))
}
override fun onFailure(p0: Call<S>, p1: Throwable) {
responseInstance.success = false
responseInstance.errorData = ErrorResponse.NetworkError(p1)
callback.onResponse(this@ResponseCall, retrofit2.Response.success(responseInstance))
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment