Optimize the error message

Signed-off-by: chengdehao <dehaocheng@yunify.com>
This commit is contained in:
chengdehao
2022-01-24 16:52:02 +08:00
parent 77d14fe4dc
commit 37369334f1
5 changed files with 44 additions and 10 deletions

View File

@@ -28,6 +28,8 @@ import (
"github.com/elastic/go-elasticsearch/v5"
"github.com/elastic/go-elasticsearch/v5/esapi"
"kubesphere.io/kubesphere/pkg/simple/client/es/versions"
)
type Elastic struct {
@@ -112,12 +114,15 @@ func (e *Elastic) GetTotalHitCount(v interface{}) int64 {
}
func parseError(response *esapi.Response) error {
var e map[string]interface{}
var e versions.Error
if err := json.NewDecoder(response.Body).Decode(&e); err != nil {
return err
} else {
// Print the response status and error information.
e, _ := e["error"].(map[string]interface{})
return fmt.Errorf("type: %v, reason: %v", e["type"], e["reason"])
if len(e.Details.RootCause) != 0 {
return fmt.Errorf("type: %v, reason: %v", e.Details.Type, e.Details.RootCause[0].Reason)
} else {
return fmt.Errorf("type: %v, reason: %v", e.Details.Type, e.Details.Reason)
}
}
}