2
votes

I'm calling an async call using Retrofit inside an Anko doAsync call. The problem is that I don't know how to catch the IOException that throws the execute() properly, basically the execute call is not getting called. If I comment the if (resultBody is T) line, and put the call inside a trycatch it works like a charm. Anyone knows why?

doAsync {

    val response = call.execute()

    uiThread {

        if (response.isSuccessful) {

            val resultBody = response.body()

            if (resultBody is T)
                callback.onSuccess(resultBody)
        }
    }
}
1

1 Answers

2
votes

From doAsync docs:

  • @param exceptionHandler optional exception handler. * If defined, any exceptions thrown inside [task] will be passed to it. If not, exceptions will be ignored.

Looks like you can pass an exceptionHandler to manage them or it will handle exceptions with their crashLogger:

private val crashLogger = { throwable : Throwable -> throwable.printStackTrace() }

//...

fun <T> T.doAsync( //...
    exceptionHandler: ((Throwable) -> Unit)? = crashLogger,
    //... )

You can check Anko doAsync implementation for details.