[Groonga-commit] groonga/grnci at a45c285 [master] Add GroongaError, ErrorCode and ResultCode.

Back to archive index

Susumu Yata null+****@clear*****
Tue Jul 11 13:57:13 JST 2017


Susumu Yata	2017-07-11 13:57:13 +0900 (Tue, 11 Jul 2017)

  New Revision: a45c28555caf5dcc80b8f096df1afe469c0791eb
  https://github.com/groonga/grnci/commit/a45c28555caf5dcc80b8f096df1afe469c0791eb

  Message:
    Add GroongaError, ErrorCode and ResultCode.

  Modified files:
    v2/error.go
    v2/error_test.go
    v2/gqtp.go
    v2/http.go
    v2/libgrn/libgrn.go

  Modified: v2/error.go (+69 -29)
===================================================================
--- v2/error.go    2017-07-11 11:02:23 +0900 (14a3550)
+++ v2/error.go    2017-07-11 13:57:13 +0900 (9fef5fa)
@@ -2,25 +2,71 @@ package grnci
 
 import (
 	"encoding/json"
+	"strconv"
 )
 
-// Error codes.
+// ErrorCode is an error code.
+type ErrorCode int
+
+// List of error codes.
 const (
-	AddressError = 1000 + iota
+	AddressError = ErrorCode(1 + iota)
 	CommandError
 	OperationError
 	ResponseError
 	TypeError
 	NetworkError
 	HTTPError
+	GroongaError
 	UnknownError
 )
 
-// getCodeText returns a string that briefly describes the specified code.
-// getCodeText supports Groonga result codes (C.grn_rc) [,0] and
-// Grnci error codes [1000,].
-func getCodeText(code int) string {
-	switch code {
+// Name returns the name of the error code.
+func (ec ErrorCode) Name() string {
+	switch ec {
+	case AddressError:
+		return "AddressError"
+	case CommandError:
+		return "CommandError"
+	case OperationError:
+		return "OperationError"
+	case ResponseError:
+		return "ResponseError"
+	case TypeError:
+		return "TypeError"
+	case NetworkError:
+		return "NetworkError"
+	case HTTPError:
+		return "HTTPError"
+	case GroongaError:
+		return "GroongaError"
+	case UnknownError:
+		return "UnknownError"
+	default:
+		return "N/A"
+	}
+}
+
+// String returns a string that consists of the error code and its name.
+func (ec ErrorCode) String() string {
+	return strconv.Itoa(int(ec)) + " " + ec.Name()
+}
+
+// MarshalJSON returns the JSON-encoded error code.
+func (ec ErrorCode) MarshalJSON() ([]byte, error) {
+	buf := make([]byte, 0, 24)
+	buf = strconv.AppendInt(buf, int64(ec), 10)
+	buf = append(buf, ' ')
+	buf = append(buf, ec.Name()...)
+	return buf, nil
+}
+
+// ResultCode is a Groons result code.
+type ResultCode int
+
+// Name returns the name of the result code.
+func (rc ResultCode) Name() string {
+	switch rc {
 	case 0:
 		return "GRN_SUCCESS"
 	case 1:
@@ -183,41 +229,35 @@ func getCodeText(code int) string {
 		return "GRN_WINDOW_FUNCTION_ERROR"
 	case -79:
 		return "GRN_ZSTD_ERROR"
-
-	case AddressError:
-		return "AddressError"
-	case CommandError:
-		return "CommandError"
-	case OperationError:
-		return "OperationError"
-	case ResponseError:
-		return "ResponseError"
-	case TypeError:
-		return "TypeError"
-	case NetworkError:
-		return "NetworkError"
-	case HTTPError:
-		return "HTTPError"
-	case UnknownError:
-		return "UnknownError"
-
 	default:
 		return "N/A"
 	}
 }
 
+// String returns a string that consists of the result code and its name.
+func (rc ResultCode) String() string {
+	return strconv.Itoa(int(rc)) + " " + rc.Name()
+}
+
+// MarshalJSON returns the JSON-encoded error code.
+func (rc ResultCode) MarshalJSON() ([]byte, error) {
+	buf := make([]byte, 0, 24)
+	buf = strconv.AppendInt(buf, int64(rc), 10)
+	buf = append(buf, ' ')
+	buf = append(buf, rc.Name()...)
+	return buf, nil
+}
+
 // Error stores an error.
 type Error struct {
-	Code int                    `json:"code"`
-	Text string                 `json:"text"`
+	Code ErrorCode              `json:"code"`
 	Data map[string]interface{} `json:"data,omitempty"`
 }
 
 // NewError returns a new Error.
-func NewError(code int, data map[string]interface{}) *Error {
+func NewError(code ErrorCode, data map[string]interface{}) *Error {
 	err := &Error{
 		Code: code,
-		Text: getCodeText(code),
 		Data: make(map[string]interface{}),
 	}
 	for k, v := range data {

  Modified: v2/error_test.go (+0 -8)
===================================================================
--- v2/error_test.go    2017-07-11 11:02:23 +0900 (785393b)
+++ v2/error_test.go    2017-07-11 13:57:13 +0900 (2729e3d)
@@ -12,10 +12,6 @@ func TestNewError(t *testing.T) {
 		t.Fatalf("NewError failed: Code: actual = %d, want = %d",
 			err.Code, AddressError)
 	}
-	if err.Text != getCodeText(AddressError) {
-		t.Fatalf("NewError failed: Text: actual = %s, want = %s",
-			err.Text, getCodeText(AddressError))
-	}
 	for k, v := range data {
 		if err.Data[k] != v {
 			t.Fatalf("NewError failed: Data[\"key\"]: actual = %s, want = %s", err.Data[k], v)
@@ -39,10 +35,6 @@ func TestEnhanceError(t *testing.T) {
 		t.Fatalf("NewError failed: Code: actual = %d, want = %d",
 			err.Code, AddressError)
 	}
-	if err.Text != getCodeText(AddressError) {
-		t.Fatalf("NewError failed: Text: actual = %s, want = %s",
-			err.Text, getCodeText(AddressError))
-	}
 	for k, v := range newData {
 		if err.Data[k] != v {
 			t.Fatalf("NewError failed: Data[\"key\"]: actual = %s, want = %s", err.Data[k], v)

  Modified: v2/gqtp.go (+4 -2)
===================================================================
--- v2/gqtp.go    2017-07-11 11:02:23 +0900 (ae1dc65)
+++ v2/gqtp.go    2017-07-11 13:57:13 +0900 (c6c16b2)
@@ -69,8 +69,10 @@ func newGQTPResponse(conn *GQTPConn, head gqtpHeader, start time.Time, name stri
 		left:    int(head.Size),
 	}
 	if head.Status > 32767 {
-		code := int(head.Status) - 65536
-		resp.err = NewError(code, nil)
+		rc := int(head.Status) - 65536
+		resp.err = NewError(GroongaError, map[string]interface{}{
+			"rc": ResultCode(rc),
+		})
 	}
 	return resp
 }

  Modified: v2/http.go (+3 -1)
===================================================================
--- v2/http.go    2017-07-11 11:02:23 +0900 (f73c617)
+++ v2/http.go    2017-07-11 13:57:13 +0900 (c704303)
@@ -85,7 +85,9 @@ Loop:
 
 // parseHTTPResponseHeaderError parses the error information in the HTTP resonse header.
 func parseHTTPResponseHeaderError(code int, elems []interface{}) error {
-	err := NewError(code, nil)
+	err := NewError(GroongaError, map[string]interface{}{
+		"rc": ResultCode(code),
+	})
 	if len(elems) >= 1 {
 		err = EnhanceError(err, map[string]interface{}{
 			"message": elems[0],

  Modified: v2/libgrn/libgrn.go (+10 -5)
===================================================================
--- v2/libgrn/libgrn.go    2017-07-11 11:02:23 +0900 (48a0d18)
+++ v2/libgrn/libgrn.go    2017-07-11 13:57:13 +0900 (d7d1e5e)
@@ -42,7 +42,8 @@ func Init() error {
 	defer libMutex.Unlock()
 	if !initFinDisabled && libCount == 0 {
 		if rc := C.grn_init(); rc != C.GRN_SUCCESS {
-			return grnci.NewError(int(rc), map[string]interface{}{
+			return grnci.NewError(grnci.GroongaError, map[string]interface{}{
+				"rc":     grnci.ResultCode(rc),
 				"method": "C.grn_init",
 				"error":  "Failed to initialize libgroonga.",
 			})
@@ -71,7 +72,8 @@ func Fin() error {
 	libCount--
 	if !initFinDisabled && libCount == 0 {
 		if rc := C.grn_fin(); rc != C.GRN_SUCCESS {
-			return grnci.NewError(int(rc), map[string]interface{}{
+			return grnci.NewError(grnci.GroongaError, map[string]interface{}{
+				"rc":     grnci.ResultCode(rc),
 				"method": "C.grn_fin",
 				"error":  "Failed to finalize libgroonga.",
 			})
@@ -110,7 +112,8 @@ func newGrnCtx() (*grnCtx, error) {
 // Close closes the grnCtx.
 func (c *grnCtx) Close() error {
 	if rc := C.grn_ctx_close(c.ctx); rc != C.GRN_SUCCESS {
-		return grnci.NewError(int(rc), map[string]interface{}{
+		return grnci.NewError(grnci.GroongaError, map[string]interface{}{
+			"rc":     grnci.ResultCode(rc),
 			"method": "C.grn_ctx_close",
 		})
 	}
@@ -126,6 +129,7 @@ func (c *grnCtx) Err(method string) error {
 		return nil
 	}
 	data := map[string]interface{}{
+		"rc":     grnci.ResultCode(c.ctx.rc),
 		"method": method,
 	}
 	if c.ctx.errline != 0 {
@@ -140,7 +144,7 @@ func (c *grnCtx) Err(method string) error {
 	if c.ctx.errbuf[0] != 0 {
 		data["error"] = C.GoString(&c.ctx.errbuf[0])
 	}
-	return grnci.NewError(int(c.ctx.rc), data)
+	return grnci.NewError(grnci.GroongaError, data)
 }
 
 // Send sends data with flags.
@@ -240,7 +244,8 @@ func (db *grnDB) Close(ctx *grnCtx) error {
 					"rc": int(rc),
 				})
 			}
-			return grnci.NewError(int(rc), map[string]interface{}{
+			return grnci.NewError(grnci.GroongaError, map[string]interface{}{
+				"rc":     grnci.ResultCode(rc),
 				"method": "C.grn_obj_close",
 			})
 		}
-------------- next part --------------
HTML����������������������������...
Download 



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