Comments (11)
I have not been able to resolve this and its making be doubt my use of coroutines. Nothing i have tried has helped catch exceptions(Any) that occur in the interceptor. If anyone has a working solution that would be fantastic.
from retrofit2-kotlin-coroutines-adapter.
I have the same problem :S
from retrofit2-kotlin-coroutines-adapter.
You could try to get the job reference of your async task and setup a invokeOnCompletion
callback, I haven't used this the library yet but have had similar problems with Differed<T>
in the past. So perhaps try this:
async {
val request = retrofitBuildService.service.downloadPDF(codeType, uuid)
val response = request.await()
if (response.isSuccessful) {
if (response.headers().get("Content-Type").equals(PDF_CONTENT_TYPE)) {
return writeToFile(response.body()!!)
} else {
val responseText = response.body()!!.string()
JSONParser.jsonErrorParser(responseText)
}
} else
throw ResponseException(response.code())
}.invokeOnCompletion {
it?.printStackTrace()
}
I hope this helps, just a reminder that the invokeOnCompletion
function is not executed on the main thread so you'd need to do something like withContext(UI)
or withContext(Dispatchers.Main)
from retrofit2-kotlin-coroutines-adapter.
Also can't catch SocketTimeoutException...
from retrofit2-kotlin-coroutines-adapter.
@JakeWharton
Please explain what the differences are in your implementation.
adapt (call: Call <T>): Deferred <T>
from the following:
override fun adapt(call: Call<T>): Deferred<T> = GlobalScope.async(IO) { call.execute().body()!! }
in addition to tight binding to the IO context. Also, this approach will throw SocketTimeoutException type exceptions into Deferred and can be handled along with errors that the API throws without any additional effort.
from retrofit2-kotlin-coroutines-adapter.
@GrachevVladimiw
execute -> Synchronously send the request and return its response
enqueue -> Asynchronously send the request and notify callback of its response
You can't cancel Call when you use execute. If you use enqueue and try cancel Deferred, Call will be canceled, but with execute you can't cancel executing the call.
from retrofit2-kotlin-coroutines-adapter.
@vitoksmile Thank you very much!
from retrofit2-kotlin-coroutines-adapter.
For tests:
use MockWebServer, throttleBody period must be greater than okhttpclient timeout and you got SocketTimeOutException
from retrofit2-kotlin-coroutines-adapter.
@badvok666 please read my answer in #3 (comment)
from retrofit2-kotlin-coroutines-adapter.
@badvok666 please read my answer in #3 (comment)
We have another problem. Try can't catch timeout exception at all
from retrofit2-kotlin-coroutines-adapter.
@KostyaM check my update! I had added catch (se3: SocketTimeoutException)
from retrofit2-kotlin-coroutines-adapter.
Related Issues (20)
- Null properties in the returned object HOT 1
- Compiled by a pre-release version HOT 2
- [ENHANCEMENT]ReceiveChannel or produce-consume pattern HOT 1
- Gradle and Java 6? HOT 1
- [OPTIMIZE]Why no variables to store versions of core libraries? HOT 1
- Handle null body on 204 no content response HOT 1
- Cancelling a Job, doesn't cancel the call HOT 1
- Is the adapter working with Kotlin 1.3? HOT 2
- Coroutines 1.0.0 HOT 1
- Why do you use call.enqueue() instead of call.execute() in CallAdapter? HOT 3
- Using `Deferred` type with `BehaviorDelegate`? HOT 4
- okhttp interceptor throw SocketTimeoutException caused the crash HOT 7
- Fire and forget HOT 1
- Thread is different first and next HOT 1
- Retrieve call URL before we make the request HOT 1
- ClassCastException: kotlin.Result$Failure cannot be cast to retrofit2.Response HOT 1
- Can't catch exceptions HOT 2
- Cancel Coroutine would cancel the call? HOT 1
- Is it possible to know the called method when an error occurred? HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from retrofit2-kotlin-coroutines-adapter.