Giter VIP home page Giter VIP logo

Comments (11)

badvok666 avatar badvok666 commented on June 30, 2024 1

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.

OscarGonzalezLesaga avatar OscarGonzalezLesaga commented on June 30, 2024

I have the same problem :S

from retrofit2-kotlin-coroutines-adapter.

wax911 avatar wax911 commented on June 30, 2024

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.

GrachevVladimiw avatar GrachevVladimiw commented on June 30, 2024

Also can't catch SocketTimeoutException...

from retrofit2-kotlin-coroutines-adapter.

GrachevVladimiw avatar GrachevVladimiw commented on June 30, 2024

@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.

vitoksmile avatar vitoksmile commented on June 30, 2024

@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.

GrachevVladimiw avatar GrachevVladimiw commented on June 30, 2024

@vitoksmile Thank you very much!

from retrofit2-kotlin-coroutines-adapter.

marinat avatar marinat commented on June 30, 2024

For tests:
use MockWebServer, throttleBody period must be greater than okhttpclient timeout and you got SocketTimeOutException

from retrofit2-kotlin-coroutines-adapter.

Studentessa avatar Studentessa commented on June 30, 2024

@badvok666 please read my answer in #3 (comment)

from retrofit2-kotlin-coroutines-adapter.

KostyaM avatar KostyaM commented on June 30, 2024

@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.

Studentessa avatar Studentessa commented on June 30, 2024

@KostyaM check my update! I had added catch (se3: SocketTimeoutException)

from retrofit2-kotlin-coroutines-adapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.