diff --git a/elastic/errors.go b/elastic/errors.go index 670a275..f6b5281 100644 --- a/elastic/errors.go +++ b/elastic/errors.go @@ -1,9 +1,9 @@ package elastic import ( - "encoding/json" "errors" "fmt" + "io" "github.com/elastic/go-elasticsearch/v7/esapi" ) @@ -11,19 +11,6 @@ import ( // ErrQueryTimeout is returned when the search query has timed out. var ErrQueryTimeout = errors.New("query timeout") -// dumpRootCauses returns a string representation of all root_cause entries as: -// "type1: reason1; type2: reason2; ...; typeN: reasonN" -func dumpRootCauses(rcs []interface{}) string { - var rcsStr string - for _, rc := range rcs { - rcsStr += fmt.Sprintf( - "; %s: %s", - rc.(map[string]interface{})["type"], - rc.(map[string]interface{})["reason"]) - } - return rcsStr -} - // IsAPIError checks if an es response contains an error. // If so, it decodes the body and returns the error. func IsAPIError(res *esapi.Response) error { @@ -31,20 +18,8 @@ func IsAPIError(res *esapi.Response) error { return nil } - var e map[string]interface{} - if err := json.NewDecoder(res.Body).Decode(&e); err != nil { - return err - } - - if e["error"].(map[string]interface{})["root_cause"] != nil { - return fmt.Errorf("[%s] %s: %s%s", - res.Status(), - e["error"].(map[string]interface{})["type"], - e["error"].(map[string]interface{})["reason"], - dumpRootCauses(e["error"].(map[string]interface{})["root_cause"].([]interface{}))) - } - return fmt.Errorf("[%s] %s: %s", - res.Status(), - e["error"].(map[string]interface{})["type"], - e["error"].(map[string]interface{})["reason"]) + // read body and return as error + errMsg := make([]byte, 1024) + errMsgSize, _ := io.ReadFull(res.Body, errMsg) + return fmt.Errorf("[%s] %s", res.Status(), errMsg[:errMsgSize]) }