apollo-kotlin

How to get the error body from an ApolloHttpException?


With Apollo Kotlin 3 the ApolloHttpException there is no rawResponse() method anymore and the body property is always null.

Is there a way to get the error body?


Solution

  • As stated in the documentation, the HTTP error body, by default, is always null. You can opt-in exposeHttpErrorBody in HttpNetworkTransport if you need it. If you're doing this, you must call BufferedSource.close on body to avoid sockets and other resources leaking.

    To do this, when you are creating the Apollo client, you have to set .httpExposeErrorBody() to true:

    ApolloClient.Builder()
                .serverUrl(graphQlUrl)
                .httpExposeErrorBody(true)
                .build()
    

    If you're setting it to true, you must catch ApolloHttpException and close the body explicitly to avoid sockets and other resources leaking.