fuel icon indicating copy to clipboard operation
fuel copied to clipboard

java.io.IOException when getting data from error response

Open adriamt opened this issue 5 years ago • 3 comments

Hi, I have implemented a simple post to an api to make login

return runBlocking {
            val (request, response, result) =  manager.post(URL + resource, params).awaitStringResponseResult()
            result.fold(
                { data ->
                    println(data)
                    return@runBlocking ApiResponse(data,null)
                },
                { error ->
                    return@runBlocking ApiResponse(null,apiResponse.error)
                }
            )
        }

When the response is ok, there is no problem. The problem occurs when I try to get the data response from an error request. The data that I get is this :

{
    "code": 10,
    "error": "These credentials do not match our records."
}

And this comes with an 401 Unauthorised response. I've just want to get the message from de request. If I try response.data it throws Method threw 'java.io.IOException If I try result.component()2.response it throws Method threw 'android.os.NetworkOnMainThreadException' exception. Cannot evaluate com.github.kittinunf.fuel.core.Response.toString() If I try result.error.errorData it throws Method threw 'java.io.IOException

Here is the exmple post with postman image

Any clue how can I get the response?

adriamt avatar Jun 19 '20 10:06 adriamt

This only happens when I run it with blocking. If I run it with the async I can do String(result.error.response.data) and I get the message that I need

adriamt avatar Jun 19 '20 13:06 adriamt

New advance!! If I put this to the fuel manager, it works fine manager.addResponseInterceptor(LogResponseInterceptor)

adriamt avatar Jun 23 '20 08:06 adriamt

New advance!! If I put this to the fuel manager, it works fine manager.addResponseInterceptor(LogResponseInterceptor)

This is really weird. I'm using RX extension for Fuel and also have that problem when receive something what isn't 200. With LogResponseInterceptor looks good (thanks for that hack @adriamt) but seems like that's bug. Even if I put whole RX stream to I/O thread, receiving NetworkOnMainThreadException.

Edit Looks like the reason for this exception lie here: https://github.com/kittinunf/fuel/issues/742#issuecomment-629229730

lbasek avatar Sep 08 '20 09:09 lbasek