我正在使用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)
我怎样才能看到真正的错误信息?提前感谢。
2条答案
按热度按时间sczxawaw1#
我通过查看
response.data
解决了我的问题,这是一个字节数组。通过String(response.data)
将其转换为字符串,我可以看到错误消息。hmtdttj42#
默认异常并不只是打印这个,这真的很烦人。
我努力使用泛型将处理程序拉到它自己的函数中,所以我想在这里分享我的完整代码。