Explorar o código

fix(cli): improve error handling

bergquist %!s(int64=9) %!d(string=hai) anos
pai
achega
6e65c93203
Modificáronse 1 ficheiros con 11 adicións e 7 borrados
  1. 11 7
      pkg/cmd/grafana-cli/services/services.go

+ 11 - 7
pkg/cmd/grafana-cli/services/services.go

@@ -35,11 +35,11 @@ func Init(version string) {
 }
 
 func ListAllPlugins(repoUrl string) (m.PluginRepo, error) {
-	body, err := createRequest(repoUrl, "repo")
+	body, err := sendRequest(repoUrl, "repo")
 
 	if err != nil {
-		logger.Info("Failed to create request", "error", err)
-		return m.PluginRepo{}, fmt.Errorf("Failed to create request. error: %v", err)
+		logger.Info("Failed to send request", "error", err)
+		return m.PluginRepo{}, fmt.Errorf("Failed to send request. error: %v", err)
 	}
 
 	if err != nil {
@@ -112,11 +112,11 @@ func RemoveInstalledPlugin(pluginPath, pluginName string) error {
 }
 
 func GetPlugin(pluginId, repoUrl string) (m.Plugin, error) {
-	body, err := createRequest(repoUrl, "repo", pluginId)
+	body, err := sendRequest(repoUrl, "repo", pluginId)
 
 	if err != nil {
-		logger.Info("Failed to create request", "error", err)
-		return m.Plugin{}, fmt.Errorf("Failed to create request. error: %v", err)
+		logger.Info("Failed to send request", "error", err)
+		return m.Plugin{}, fmt.Errorf("Failed to send request. error: %v", err)
 	}
 
 	if err != nil {
@@ -133,7 +133,7 @@ func GetPlugin(pluginId, repoUrl string) (m.Plugin, error) {
 	return data, nil
 }
 
-func createRequest(repoUrl string, subPaths ...string) ([]byte, error) {
+func sendRequest(repoUrl string, subPaths ...string) ([]byte, error) {
 	u, _ := url.Parse(repoUrl)
 	for _, v := range subPaths {
 		u.Path = path.Join(u.Path, v)
@@ -149,6 +149,10 @@ func createRequest(repoUrl string, subPaths ...string) ([]byte, error) {
 	}
 
 	res, err := HttpClient.Do(req)
+	if err != nil {
+		return []byte{}, err
+	}
+
 	if res.StatusCode/100 != 2 {
 		return []byte{}, fmt.Errorf("Api returned invalid status: %s", res.Status)
 	}