handle error for type assertion
Signed-off-by: huanggze <loganhuang@yunify.com>
This commit is contained in:
@@ -56,15 +56,17 @@ func detectVersionMajor(cfg *Config) error {
|
|||||||
}
|
}
|
||||||
if res.IsError() {
|
if res.IsError() {
|
||||||
// Print the response status and error information.
|
// Print the response status and error information.
|
||||||
return fmt.Errorf("[%s] %s: %s",
|
e, _ := b["error"].(map[string]interface{})
|
||||||
res.Status(),
|
return fmt.Errorf("[%s] %s: %s", res.Status(), e["type"], e["reason"])
|
||||||
b["error"].(map[string]interface{})["type"],
|
|
||||||
b["error"].(map[string]interface{})["reason"],
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the major version
|
// get the major version
|
||||||
version := fmt.Sprintf("%v", b["version"].(map[string]interface{})["number"])
|
version, _ := b["version"].(map[string]interface{})
|
||||||
cfg.VersionMajor = strings.Split(version, ".")[0]
|
number, _ := version["number"].(string)
|
||||||
|
if number == "" {
|
||||||
|
return fmt.Errorf("failed to detect elastic version number")
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.VersionMajor = strings.Split(number, ".")[0]
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,11 +41,8 @@ func (e Elastic) Search(body []byte) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
// Print the response status and error information.
|
// Print the response status and error information.
|
||||||
return nil, fmt.Errorf("[%s] %s: %s",
|
e, _ := e["error"].(map[string]interface{})
|
||||||
response.Status(),
|
return nil, fmt.Errorf("[%s] %s: %s", response.Status(), e["type"], e["reason"])
|
||||||
e["error"].(map[string]interface{})["type"],
|
|
||||||
e["error"].(map[string]interface{})["reason"],
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,5 +50,6 @@ func (e Elastic) Search(body []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e Elastic) GetTotalHitCount(v interface{}) int64 {
|
func (e Elastic) GetTotalHitCount(v interface{}) int64 {
|
||||||
return int64(v.(float64))
|
f, _ := v.(float64)
|
||||||
|
return int64(f)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,11 +41,8 @@ func (e Elastic) Search(body []byte) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
// Print the response status and error information.
|
// Print the response status and error information.
|
||||||
return nil, fmt.Errorf("[%s] %s: %s",
|
e, _ := e["error"].(map[string]interface{})
|
||||||
response.Status(),
|
return nil, fmt.Errorf("[%s] %s: %s", response.Status(), e["type"], e["reason"])
|
||||||
e["error"].(map[string]interface{})["type"],
|
|
||||||
e["error"].(map[string]interface{})["reason"],
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,5 +50,6 @@ func (e Elastic) Search(body []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e Elastic) GetTotalHitCount(v interface{}) int64 {
|
func (e Elastic) GetTotalHitCount(v interface{}) int64 {
|
||||||
return int64(v.(float64))
|
f, _ := v.(float64)
|
||||||
|
return int64(f)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,11 +42,8 @@ func (e Elastic) Search(body []byte) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
// Print the response status and error information.
|
// Print the response status and error information.
|
||||||
return nil, fmt.Errorf("[%s] %s: %s",
|
e, _ := e["error"].(map[string]interface{})
|
||||||
response.Status(),
|
return nil, fmt.Errorf("[%s] %s: %s", response.Status(), e["type"], e["reason"])
|
||||||
e["error"].(map[string]interface{})["type"],
|
|
||||||
e["error"].(map[string]interface{})["reason"],
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,5 +51,7 @@ func (e Elastic) Search(body []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e Elastic) GetTotalHitCount(v interface{}) int64 {
|
func (e Elastic) GetTotalHitCount(v interface{}) int64 {
|
||||||
return int64(v.(map[string]interface{})["value"].(float64))
|
m, _ := v.(map[string]interface{})
|
||||||
|
f, _ := m["value"].(float64)
|
||||||
|
return int64(f)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user