Created
June 26, 2022 14:33
-
-
Save 0xZhangKe/015fe07dc3dbe30f5c59049b24bc3e52 to your computer and use it in GitHub Desktop.
This file contains hidden or 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<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