httpelasticsearchkotlinfuel

Cannot see error message in http response in Kotlin Fuel


I am sending an HTTP PUT request to my Elasticsearch server using Fuel library on Kotlin. However, I cannot see an error body if the server returns 404 or 400. I am expecting to get an error message similar to the following:

{
  "error": {
    "root_cause": [
      {
        "type": "invalid_snapshot_name_exception",
        "reason": "[snap1:kopya3]Invalid snapshot name [kopya3], snapshot with the same name already exists"
      }
    ],
    "type": "invalid_snapshot_name_exception",
    "reason": "[snap1:kopya3]Invalid snapshot name [kopya3], snapshot with the same name already exists"
  },
  "status": 400
}

Here's my code:

        val (request, response, result) = fullUrl
            .httpPut()
            .body(payload)
            .responseString()

        val (bytes, error) = result

        print(error)

Instead what I see is:

HTTP Exception 400 Bad Request
    com.github.kittinunf.fuel.core.FuelError$Companion.wrap(FuelError.kt:84)
    com.github.kittinunf.fuel.core.DeserializableKt.response(Deserializable.kt:168)
    com.github.kittinunf.fuel.core.requests.DefaultRequest.responseString(DefaultRequest.kt:475)
    com.a.b.c.d.model.Cluster.createSnapshot(Cluster.kt:67)
    com.a.b.c.d.model.Cluster.createSnapshot$default(Cluster.kt:57)
    com.a.b.c.d.model.ClusterKt.main(Cluster.kt:85)
Caused by: HTTP Exception 400 Bad Request
    com.github.kittinunf.fuel.core.FuelError$Companion.wrap(FuelError.kt:86)
Caused by: com.github.kittinunf.fuel.core.HttpException: HTTP Exception 400 Bad Request
    com.github.kittinunf.fuel.core.requests.RequestTask.prepareResponse(RequestTask.kt:35)
    com.github.kittinunf.fuel.core.requests.RequestTask.call(RequestTask.kt:47)
    com.github.kittinunf.fuel.core.requests.RequestTask.call(RequestTask.kt:14)
    com.github.kittinunf.fuel.core.DeserializableKt.response(Deserializable.kt:166)
    com.github.kittinunf.fuel.core.requests.DefaultRequest.responseString(DefaultRequest.kt:475)
    com.a.b.c.d.model.Cluster.createSnapshot(Cluster.kt:67)
    com.a.b.c.d.model.Cluster.createSnapshot$default(Cluster.kt:57)
    com.a.b.c.d.model.ClusterKt.main(Cluster.kt:85)

How can I see the actual error message? Thanks in advance.


Solution

  • I solved my problem by looking at response.data which is a bytes array. Converting it to string by String(response.data) I could see the error message.