Merge pull request #4643 from wenchajun/eserror

Optimize the error message
This commit is contained in:
KubeSphere CI Bot
2022-01-28 11:04:40 +08:00
committed by GitHub
5 changed files with 44 additions and 10 deletions

View File

@@ -0,0 +1,19 @@
package versions
type Error struct {
Status int `json:"status"`
Details *ErrorDetails `json:"error,omitempty"`
}
type ErrorDetails struct {
Type string `json:"type"`
Reason string `json:"reason"`
ResourceType string `json:"resource.type,omitempty"`
ResourceId string `json:"resource.id,omitempty"`
Index string `json:"index,omitempty"`
Phase string `json:"phase,omitempty"`
Grouped bool `json:"grouped,omitempty"`
CausedBy map[string]interface{} `json:"caused_by,omitempty"`
RootCause []*ErrorDetails `json:"root_cause,omitempty"`
FailedShards []map[string]interface{} `json:"failed_shards,omitempty"`
}

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)
}
}
}

View File

@@ -28,6 +28,8 @@ import (
"github.com/elastic/go-elasticsearch/v6"
"github.com/elastic/go-elasticsearch/v6/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)
}
}
}

View File

@@ -28,6 +28,8 @@ import (
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"kubesphere.io/kubesphere/pkg/simple/client/es/versions"
)
type Elastic struct {
@@ -115,12 +117,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)
}
}
}

View File

@@ -133,7 +133,7 @@ func TestCountLogsByInterval(t *testing.T) {
fakeVersion: es.ElasticV7,
fakeResp: "es7_count_logs_by_interval_400.json",
fakeCode: http.StatusBadRequest,
expectedErr: "type: search_phase_execution_exception, reason: all shards failed",
expectedErr: "type: search_phase_execution_exception, reason: Unable to parse interval [30m0s]",
},
{
fakeVersion: es.ElasticV7,