ソースを参照

完善错误处理

wulei 1 年間 前
コミット
6fb07747b8

+ 21 - 5
datacenter_client/basic.go

@@ -40,22 +40,29 @@ func (d *DcApiClient) RequestMiddleProcess(h *httplib.HTTPRequest, projectId str
 	ts := time.Now().Unix()
 	h.Param("project_id", projectId)
 	h.Param("ts", fmt.Sprintf("%d", ts))
-	sign := d.CreateSign(h)
+	sign, err := d.CreateSign(h)
+	if err != nil {
+		return nil, err
+	}
 	h.Param("sign", sign)
 	return h.Bytes()
 }
 
-func (d *DcApiClient)CreateSign(h *httplib.HTTPRequest) string {
+func (d *DcApiClient)CreateSign(h *httplib.HTTPRequest) (string, error) {
 	s2, err := parseBody(h)
 	if err != nil {
 		fmt.Println("parseBody Error: ", err.Error())
+		return "", err
 	}
 	contentLength := h.GetRequest().ContentLength
 	if contentLength > S2_MIN_LENGTH {
 		h.Param("sign_flag", "1")
 		s2 = cutS2(s2)
 	} else {
-		s2, _ = sortS2(s2)
+		s2, err = sortS2(s2)
+		if err != nil {
+			return "", err
+		}
 	}
 
 
@@ -69,10 +76,13 @@ func (d *DcApiClient)CreateSign(h *httplib.HTTPRequest) string {
 	//fmt.Println("s2", s2)
 	//fmt.Println("s3", s3)
 	//fmt.Println("md5", md5Str)
-	return md5Str
+	return md5Str, nil
 }
 
 func cutS2(s2 string) string {
+	if s2 == "" {
+		return ""
+	}
 	s2l := len(s2)
 	//if s2l <= S2_MIN_LENGTH {
 	//	return s2
@@ -81,6 +91,9 @@ func cutS2(s2 string) string {
 }
 
 func sortS2(s2 string) (string, error) {
+	if s2 == "" {
+		return "", nil
+	}
 	var mi map[string]interface{}
 	if err := json.Unmarshal([]byte(s2), &mi); err != nil {
 		return "", err
@@ -101,7 +114,10 @@ func parseBody(r *httplib.HTTPRequest) (s2 string, err error) {
 	}
 	body := r.GetRequest().Body
 
-	cnt, _ := ioutil.ReadAll(body)
+	cnt, err := ioutil.ReadAll(body)
+	if err != nil {
+		return "", err
+	}
 	r.GetRequest().Body = ioutil.NopCloser(bytes.NewReader(cnt))
 
 	return string(cnt), nil

+ 2 - 2
datacenter_client/history_info.go

@@ -42,13 +42,13 @@ func (d *DcApiClient)GetItemHistory (req ItemHistoryReq) (resp []ItemHistoryResp
 
 	body, err := d.RequestMiddleProcess(h, req.ProjectId)
 	if err != nil {
-		log.Println("GetItemHistory Error:", err)
+		log.Println("GetItemHistory RequestMiddleProcess Error:", err)
 		return
 	}
 	result := &ItemHistoryBody{}
 	err = json.Unmarshal(body, &result)
 	if err != nil {
-		log.Println("GetItemHistory Error:", err)
+		log.Println("GetItemHistory Unmarshal Error:", err)
 		return
 	}
 	if result.Code != 200 {

+ 4 - 1
datacenter_client/multi_add.go

@@ -30,7 +30,10 @@ func (d *DcApiClient) MultiAddData (req MultiAddReq) (err error) {
 	}
 	url := fmt.Sprintf("http://%s/api/dtgateway/v1/item-history/multi-add", d.ServerIp)
 	h := httplib.Post(url)
-	h.JSONBody(req)
+	_, err = h.JSONBody(req)
+	if err != nil {
+		return
+	}
 
 	body, err := d.RequestMiddleProcess(h, fmt.Sprintf("%d", req.List[0].ProjectId))
 	if err != nil {