浏览代码

Added better error checking on the http request

Thisnthat 4 年之前
父节点
当前提交
add9ca673c
共有 1 个文件被更改,包括 15 次插入6 次删除
  1. 15 6
      about.go

+ 15 - 6
about.go

@@ -3,6 +3,7 @@ package discourse
 import (
 	"encoding/json"
 	"fmt"
+	"net/http"
 	"strings"
 )
 
@@ -24,14 +25,22 @@ func GetAbout(config APIConfig) (About, error) {
 
 	req, _ := newGetRequest(config, url)
 	client := getClient()
-	response, _ := client.Do(req)
+	response, err := client.Do(req)
 
-	var result *AboutResponse
-	json.NewDecoder(response.Body).Decode(&result)
+	if err != nil {
+		return About{}, fmt.Errorf("Unable to connect to discourse server at %s -- %s", url, err)
+	}
+
+	if response.StatusCode == http.StatusOK || response.StatusCode == http.StatusForbidden {
+		var result *AboutResponse
+		json.NewDecoder(response.Body).Decode(&result)
+
+		if result.ErrorType != "" {
+			return About{}, fmt.Errorf("Failed to get discourse version. Error: %s", strings.Join(result.Errors, "; "))
+		}
 
-	if result.ErrorType != "" {
-		return About{}, fmt.Errorf("Failed to get discourse version. Error: %s", strings.Join(result.Errors, "; "))
+		return result.About, nil
 	}
 
-	return result.About, nil
+	return About{}, fmt.Errorf("Unexpected response from discourse server at %s -- %s", url, response.Status)
 }