[Groonga-commit] groonga/grnci at 529a837 [master] Use NewError2 in http.go.

Back to archive index

Susumu Yata null+****@clear*****
Wed Aug 2 16:22:40 JST 2017


Susumu Yata	2017-08-02 16:22:40 +0900 (Wed, 02 Aug 2017)

  New Revision: 529a837661b29beef811893815ca72bbb62a19cf
  https://github.com/groonga/grnci/commit/529a837661b29beef811893815ca72bbb62a19cf

  Message:
    Use NewError2 in http.go.

  Modified files:
    v2/http.go

  Modified: v2/http.go (+35 -53)
===================================================================
--- v2/http.go    2017-08-02 16:07:19 +0900 (b77ba1c)
+++ v2/http.go    2017-08-02 16:22:40 +0900 (137b9a1)
@@ -32,9 +32,8 @@ type httpResponse struct {
 func extractHTTPResponseHeader(data []byte) (head, left []byte, err error) {
 	left = bytes.TrimLeft(data[1:], " \t\r\n")
 	if !bytes.HasPrefix(left, []byte("[")) {
-		err = NewError(ResponseError, map[string]interface{}{
-			"data":  string(data),
-			"error": "The response does not contain a header.",
+		err = NewError2(ResponseError, "The response does not contain a header.", map[string]interface{}{
+			"data": string(data),
 		})
 		return
 	}
@@ -49,9 +48,8 @@ Loop:
 			stack = append(stack, '}')
 		case ']', '}':
 			if left[i] != stack[len(stack)-1] {
-				err = NewError(ResponseError, map[string]interface{}{
-					"data":  string(data),
-					"error": "The response header is broken.",
+				err = NewError2(ResponseError, "The response header is broken.", map[string]interface{}{
+					"data": string(data),
 				})
 				return
 			}
@@ -72,9 +70,8 @@ Loop:
 		}
 	}
 	if len(stack) != 0 {
-		err = NewError(ResponseError, map[string]interface{}{
-			"data":  string(data),
-			"error": "The response header is too long or broken.",
+		err = NewError2(ResponseError, "The response header is too long or broken.", map[string]interface{}{
+			"data": string(data),
 		})
 		return
 	}
@@ -88,7 +85,7 @@ Loop:
 
 // parseHTTPResponseHeaderError parses the error information in the HTTP resonse header.
 func parseHTTPResponseHeaderError(rc int, elems []interface{}) error {
-	err := NewError(ErrorCode(rc), nil)
+	err := NewError2(ErrorCode(rc), "Error response received.", nil)
 	if len(elems) >= 1 {
 		err.Data["message"] = elems[0]
 	}
@@ -128,41 +125,35 @@ func parseHTTPResponseHeader(resp *http.Response, data []byte) (*httpResponse, e
 	// TODO: use another JSON decoder.
 	var elems []interface{}
 	if err := json.Unmarshal(head, &elems); err != nil {
-		return nil, NewError(ResponseError, map[string]interface{}{
-			"head":   string(head),
-			"method": "json.Unmarshal",
-			"error":  err.Error(),
+		return nil, NewError2(ResponseError, "json.Unmarshal failed.", map[string]interface{}{
+			"head":  string(head),
+			"error": err.Error(),
 		})
 	}
 	if len(elems) < 3 {
-		return nil, NewError(ResponseError, map[string]interface{}{
-			"elems":  elems,
-			"method": "json.Unmarshal",
-			"error":  "Too few elements in the response header.",
+		return nil, NewError2(ResponseError, "Too few elements in the response header.", map[string]interface{}{
+			"elems": elems,
 		})
 	}
 	f, ok := elems[0].(float64)
 	if !ok {
-		return nil, NewError(ResponseError, map[string]interface{}{
+		return nil, NewError2(ResponseError, "The 1st element must be the result code (number).", map[string]interface{}{
 			"elems": elems,
-			"error": "The 1st element must be the result code (number).",
 		})
 	}
 	rc := int(f)
 	f, ok = elems[1].(float64)
 	if !ok {
-		return nil, NewError(ResponseError, map[string]interface{}{
+		return nil, NewError2(ResponseError, "The 2nd element must be the start time (number).", map[string]interface{}{
 			"elems": elems,
-			"error": "The 2nd element must be the start time (number).",
 		})
 	}
 	i, f := math.Modf(f)
 	start := time.Unix(int64(i), int64(math.Floor(f*1000000+0.5))*1000).Local()
 	f, ok = elems[2].(float64)
 	if !ok {
-		return nil, NewError(ResponseError, map[string]interface{}{
+		return nil, NewError2(ResponseError, "The 3rd element must be the elapsed time (number).", map[string]interface{}{
 			"elems": elems,
-			"error": "The 3rd element must be the elapsed time (number).",
 		})
 	}
 	elapsed := time.Duration(f * float64(time.Second))
@@ -187,9 +178,8 @@ func newHTTPResponse(resp *http.Response) (*httpResponse, error) {
 	case http.StatusBadRequest:
 	default:
 		resp.Body.Close()
-		return nil, NewError(HTTPError, map[string]interface{}{
+		return nil, NewError2(HTTPError, "The status is unexpected.", map[string]interface{}{
 			"status": fmt.Sprintf("%d %s", code, http.StatusText(code)),
-			"error":  "The status is unexpected.",
 		})
 	}
 	// Read the leading bytes to get the response header.
@@ -197,9 +187,8 @@ func newHTTPResponse(resp *http.Response) (*httpResponse, error) {
 	n, err := io.ReadFull(resp.Body, buf)
 	if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
 		resp.Body.Close()
-		return nil, NewError(NetworkError, map[string]interface{}{
-			"method": "io.ReadFull",
-			"error":  err.Error(),
+		return nil, NewError2(NetworkError, "io.ReadFull failed.", map[string]interface{}{
+			"error": err.Error(),
 		})
 	}
 	data := bytes.TrimLeft(buf[:n], " \t\r\n")
@@ -250,9 +239,8 @@ func (r *httpResponse) Read(p []byte) (n int, err error) {
 				n--
 			}
 			if err != io.EOF {
-				err = NewError(NetworkError, map[string]interface{}{
-					"method": "http.Response.Body.Read",
-					"error":  err.Error(),
+				err = NewError2(NetworkError, "http.Response.Body.Read failed.", map[string]interface{}{
+					"error": err.Error(),
 				})
 			}
 			return
@@ -270,9 +258,8 @@ func (r *httpResponse) Read(p []byte) (n int, err error) {
 		n--
 	}
 	if err != io.EOF {
-		err = NewError(NetworkError, map[string]interface{}{
-			"method": "http.Response.Body.Read",
-			"error":  err.Error(),
+		err = NewError2(NetworkError, "http.Response.Body.Read failed.", map[string]interface{}{
+			"error": err.Error(),
 		})
 	}
 	return
@@ -282,15 +269,13 @@ func (r *httpResponse) Read(p []byte) (n int, err error) {
 func (r *httpResponse) Close() error {
 	if _, err := io.Copy(ioutil.Discard, r.resp.Body); err != nil {
 		r.resp.Body.Close()
-		return NewError(NetworkError, map[string]interface{}{
-			"method": "io.Copy",
-			"error":  err.Error(),
+		return NewError2(NetworkError, "io.Copy failed.", map[string]interface{}{
+			"error": err.Error(),
 		})
 	}
 	if err := r.resp.Body.Close(); err != nil {
-		return NewError(NetworkError, map[string]interface{}{
-			"method": "http.Response.Body.Close",
-			"error":  err.Error(),
+		return NewError2(NetworkError, "http.Response.Body.Close failed.", map[string]interface{}{
+			"error": err.Error(),
 		})
 	}
 	return nil
@@ -318,10 +303,9 @@ func NewHTTPClient(addr string, client *http.Client) (*HTTPClient, error) {
 	}
 	url, err := url.Parse(a.String())
 	if err != nil {
-		return nil, NewError(AddressError, map[string]interface{}{
-			"url":    a.String(),
-			"method": "url.Parse",
-			"error":  err.Error(),
+		return nil, NewError2(AddressError, "url.Parse failed.", map[string]interface{}{
+			"url":   a.String(),
+			"error": err.Error(),
 		})
 	}
 	if client == nil {
@@ -352,20 +336,18 @@ func (c *HTTPClient) exec(name string, params map[string]string, body io.Reader)
 	if body == nil {
 		resp, err := c.client.Get(url.String())
 		if err != nil {
-			return nil, NewError(NetworkError, map[string]interface{}{
-				"url":    url.String(),
-				"method": "http.Client.Get",
-				"error":  err.Error(),
+			return nil, NewError2(NetworkError, "http.Client.Get failed.", map[string]interface{}{
+				"url":   url.String(),
+				"error": err.Error(),
 			})
 		}
 		return newHTTPResponse(resp)
 	}
 	resp, err := c.client.Post(url.String(), "application/json", body)
 	if err != nil {
-		return nil, NewError(NetworkError, map[string]interface{}{
-			"url":    url.String(),
-			"method": "http.Client.Post",
-			"error":  err.Error(),
+		return nil, NewError2(NetworkError, "http.Client.Post failed.", map[string]interface{}{
+			"url":   url.String(),
+			"error": err.Error(),
 		})
 	}
 	return newHTTPResponse(resp)
-------------- next part --------------
HTML����������������������������...
下载 



More information about the Groonga-commit mailing list
Back to archive index