[Groonga-commit] groonga/grnci at 1a0f27f [master] Add HTTPError.

Back to archive index

Susumu Yata null+****@clear*****
Tue Jul 11 10:49:59 JST 2017


Susumu Yata	2017-07-11 10:49:59 +0900 (Tue, 11 Jul 2017)

  New Revision: 1a0f27f904c2810864742925306beb9294dd7df9
  https://github.com/groonga/grnci/commit/1a0f27f904c2810864742925306beb9294dd7df9

  Message:
    Add HTTPError.

  Modified files:
    v2/error.go
    v2/http.go

  Modified: v2/error.go (+5 -6)
===================================================================
--- v2/error.go    2017-07-10 12:21:59 +0900 (6358757)
+++ v2/error.go    2017-07-11 10:49:59 +0900 (f808b46)
@@ -2,7 +2,6 @@ package grnci
 
 import (
 	"encoding/json"
-	"net/http"
 )
 
 // Error codes.
@@ -13,12 +12,13 @@ const (
 	InvalidResponse
 	InvalidType
 	NetworkError
+	HTTPError
 	UnknownError
 )
 
 // getCodeText returns a string that briefly describes the specified code.
-// getCodeText supports Groonga result codes (C.grn_rc) [,0],
-// Grnci error codes [1000,] and HTTP status codes [100,999].
+// getCodeText supports Groonga result codes (C.grn_rc) [,0] and
+// Grnci error codes [1000,].
 func getCodeText(code int) string {
 	switch code {
 	case 0:
@@ -196,13 +196,12 @@ func getCodeText(code int) string {
 		return "invalid type"
 	case NetworkError:
 		return "network error"
+	case HTTPError:
+		return "HTTP error"
 	case UnknownError:
 		return "unknown error"
 
 	default:
-		if text := http.StatusText(code); text != "" {
-			return text
-		}
 		return "undefined error"
 	}
 }

  Modified: v2/http.go (+6 -3)
===================================================================
--- v2/http.go    2017-07-10 12:21:59 +0900 (39bddd9)
+++ v2/http.go    2017-07-11 10:49:59 +0900 (76afad4)
@@ -3,6 +3,7 @@ package grnci
 import (
 	"bytes"
 	"encoding/json"
+	"fmt"
 	"io"
 	"io/ioutil"
 	"math"
@@ -203,9 +204,11 @@ func newHTTPResponse(resp *http.Response, start time.Time) (*httpResponse, error
 		return parseHTTPResponseHeader(resp, data)
 	}
 	var err error
-	if resp.StatusCode != http.StatusOK {
-		err = NewError(resp.StatusCode, map[string]interface{}{
-			"note": "The response format is not JSON.",
+	code := resp.StatusCode
+	if code != http.StatusOK {
+		err = NewError(HTTPError, map[string]interface{}{
+			"status": fmt.Sprintf("%d %s", code, http.StatusText(code)),
+			"note":   "The response format is not JSON.",
 		})
 	}
 	return &httpResponse{
-------------- next part --------------
HTML����������������������������...
Télécharger 



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