elasticsearch 无法在KotlinFuel的http响应中看到错误消息

2nc8po8w  于 2023-01-25  发布在  ElasticSearch
关注(0)|答案(2)|浏览(157)

我正在使用Kotlin上的Fuel库向我的Elasticsearch服务器发送HTTP PUT请求。但是,如果服务器返回404或400,我看不到错误正文。我希望收到类似以下的错误消息:

{
  "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
}

下面是我的代码:

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

        val (bytes, error) = result

        print(error)

相反我看到的是:

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)

我怎样才能看到真正的错误信息?提前感谢。

sczxawaw

sczxawaw1#

我通过查看response.data解决了我的问题,这是一个字节数组。通过String(response.data)将其转换为字符串,我可以看到错误消息。

hmtdttj4

hmtdttj42#

默认异常并不只是打印这个,这真的很烦人。
我努力使用泛型将处理程序拉到它自己的函数中,所以我想在这里分享我的完整代码。

fun <T> genericErrorHandler(response: Response, result: Result.Failure<Exception>): T {
        println("Request to url ${response.url} failed, server returned:")
        println(String(response.data))
        throw result.getException()
    }

    private fun someRequest(url: String, jsonBody: String): SomeDataFormatYouExpectOnSuccess {
        val (_, response, result) = url.httpPost().header("Content-Type", "application/json; utf-8")
            .header("Authorization", "") // adding some headers
            .jsonBody(jsonBody)
            .responseString()
        return when (result) {
            is Result.Failure -> genericErrorHandler(response, result)
            is Result.Success -> {
                val data = result.get()
                gson.fromJson(data, SomeDataFormatYouExpectOnSuccess::class.java)
            }
        }
    }

相关问题