Susumu Yata
null+****@clear*****
Wed Aug 2 11:20:21 JST 2017
Susumu Yata 2017-08-02 11:20:21 +0900 (Wed, 02 Aug 2017) New Revision: f47f49e686e45a0bb33b9c5bee711c3d688b4f39 https://github.com/groonga/grnci/commit/f47f49e686e45a0bb33b9c5bee711c3d688b4f39 Message: Remove ResultCode and NewGroongaError to simplify error handling. Modified files: v2/error.go v2/error_test.go v2/gqtp.go v2/http.go v2/libgrn/libgrn.go Modified: v2/error.go (+22 -59) =================================================================== --- v2/error.go 2017-08-01 16:55:54 +0900 (f60a860) +++ v2/error.go 2017-08-02 11:20:21 +0900 (d934e79) @@ -8,9 +8,9 @@ import ( // ErrorCode is an error code. type ErrorCode int -// List of error codes. +// List of error codes except Groonga result codes. const ( - AddressError = ErrorCode(1 + iota) + AddressError = ErrorCode(1000 + iota) CommandError OperationError ResponseError @@ -24,7 +24,8 @@ const ( UnexpectedError ) -// Name returns the name of the error code such as "AddressError". +// Name returns the name of the error code such as +// "AddressError" adn "GRN_UNKNOWN_ERROR". func (ec ErrorCode) Name() string { switch ec { case AddressError: @@ -49,35 +50,7 @@ func (ec ErrorCode) Name() string { return "GroongaError" case UnexpectedError: return "UnexpectedError" - default: - return "N/A" - } -} - -// String returns a string that consists of the error code and its name. -func (ec ErrorCode) String() string { - return ec.Name() + " (" + strconv.Itoa(int(ec)) + ")" -} -// MarshalJSON returns the JSON-encoded error code. -func (ec ErrorCode) MarshalJSON() ([]byte, error) { - buf := make([]byte, 0, 24) - buf = append(buf, '"') - buf = append(buf, ec.Name()...) - buf = append(buf, ' ') - buf = append(buf, '(') - buf = strconv.AppendInt(buf, int64(ec), 10) - buf = append(buf, ')') - buf = append(buf, '"') - return buf, nil -} - -// ResultCode is a Groonga result code. -type ResultCode int - -// Name returns the name of the result code such as "GRN_SUCCESS". -func (rc ResultCode) Name() string { - switch rc { case 0: return "GRN_SUCCESS" case 1: @@ -240,27 +213,31 @@ func (rc ResultCode) Name() string { return "GRN_WINDOW_FUNCTION_ERROR" case -79: return "GRN_ZSTD_ERROR" + default: - return "N/A" + return "" } } -// String returns a string that consists of the result code and its name. -func (rc ResultCode) String() string { - return rc.Name() + " (" + strconv.Itoa(int(rc)) + ")" +// String returns a string that consists of the error code and its name. +func (ec ErrorCode) String() string { + if name := ec.Name(); name != "" { + return name + } + return strconv.Itoa(int(ec)) } // MarshalJSON returns the JSON-encoded error code. -func (rc ResultCode) MarshalJSON() ([]byte, error) { - buf := make([]byte, 0, 48) - buf = append(buf, '"') - buf = append(buf, rc.Name()...) - buf = append(buf, ' ') - buf = append(buf, '(') - buf = strconv.AppendInt(buf, int64(rc), 10) - buf = append(buf, ')') - buf = append(buf, '"') - return buf, nil +func (ec ErrorCode) MarshalJSON() ([]byte, error) { + if name := ec.Name(); name != "" { + buf := make([]byte, len(name)+2) + buf[0] = '"' + copy(buf[1:], name) + buf[len(buf)-1] = '"' + return buf, nil + } + var buf []byte + return strconv.AppendInt(buf, int64(ec), 10), nil } // Error stores an error. @@ -281,20 +258,6 @@ func NewError(code ErrorCode, data map[string]interface{}) *Error { return err } -// NewGroongaError returns a new Error. -func NewGroongaError(rc ResultCode, data map[string]interface{}) *Error { - err := &Error{ - Code: GroongaError, - Data: map[string]interface{}{ - "rc": rc, - }, - } - for k, v := range data { - err.Data[k] = v - } - return err -} - // EnhanceError adds data to err and returns the modified Error. func EnhanceError(err error, data map[string]interface{}) *Error { if e, ok := err.(*Error); ok { Modified: v2/error_test.go (+2 -0) =================================================================== --- v2/error_test.go 2017-08-01 16:55:54 +0900 (6eb182a) +++ v2/error_test.go 2017-08-02 11:20:21 +0900 (2c22c1f) @@ -1,6 +1,7 @@ package grnci import ( + "log" "testing" ) @@ -18,6 +19,7 @@ func TestNewError(t *testing.T) { t.Fatalf("NewError failed: Data[\"%s\"]: actual = %s, want = %s", k, err.Data[k], v) } } + log.Printf("err = %v", err) } func TestEnhanceError(t *testing.T) { Modified: v2/gqtp.go (+1 -1) =================================================================== --- v2/gqtp.go 2017-08-01 16:55:54 +0900 (afe5d05) +++ v2/gqtp.go 2017-08-02 11:20:21 +0900 (2f0de96) @@ -63,7 +63,7 @@ func newGQTPResponse(conn *gqtpConn, head gqtpHeader) *gqtpResponse { } if head.Status > 32767 { rc := int(head.Status) - 65536 - resp.err = NewGroongaError(ResultCode(rc), nil) + resp.err = NewError(ErrorCode(rc), nil) } return resp } Modified: v2/http.go (+1 -1) =================================================================== --- v2/http.go 2017-08-01 16:55:54 +0900 (bef7206) +++ v2/http.go 2017-08-02 11:20:21 +0900 (78fa21c) @@ -88,7 +88,7 @@ Loop: // parseHTTPResponseHeaderError parses the error information in the HTTP resonse header. func parseHTTPResponseHeaderError(rc int, elems []interface{}) error { - err := NewGroongaError(ResultCode(rc), nil) + err := NewError(ErrorCode(rc), nil) if len(elems) >= 1 { err = EnhanceError(err, map[string]interface{}{ "message": elems[0], Modified: v2/libgrn/libgrn.go (+5 -5) =================================================================== --- v2/libgrn/libgrn.go 2017-08-01 16:55:54 +0900 (f3f673f) +++ v2/libgrn/libgrn.go 2017-08-02 11:20:21 +0900 (541e679) @@ -42,7 +42,7 @@ func Init() error { defer libMutex.Unlock() if !initFinDisabled && libCount == 0 { if rc := C.grn_init(); rc != C.GRN_SUCCESS { - return grnci.NewGroongaError(grnci.ResultCode(rc), map[string]interface{}{ + return grnci.NewError(grnci.ErrorCode(rc), map[string]interface{}{ "method": "C.grn_init", "error": "Failed to initialize libgroonga.", }) @@ -71,7 +71,7 @@ func Fin() error { libCount-- if !initFinDisabled && libCount == 0 { if rc := C.grn_fin(); rc != C.GRN_SUCCESS { - return grnci.NewGroongaError(grnci.ResultCode(rc), map[string]interface{}{ + return grnci.NewError(grnci.ErrorCode(rc), map[string]interface{}{ "method": "C.grn_fin", "error": "Failed to finalize libgroonga.", }) @@ -110,7 +110,7 @@ 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.NewGroongaError(grnci.ResultCode(rc), map[string]interface{}{ + return grnci.NewError(grnci.ErrorCode(rc), map[string]interface{}{ "method": "C.grn_ctx_close", }) } @@ -140,7 +140,7 @@ func (c *grnCtx) Err(method string) error { if c.ctx.errbuf[0] != 0 { data["error"] = C.GoString(&c.ctx.errbuf[0]) } - return grnci.NewGroongaError(grnci.ResultCode(c.ctx.rc), data) + return grnci.NewError(grnci.ErrorCode(c.ctx.rc), data) } // Send sends data with flags. @@ -240,7 +240,7 @@ func (db *grnDB) Close(ctx *grnCtx) error { "rc": int(rc), }) } - return grnci.NewGroongaError(grnci.ResultCode(rc), map[string]interface{}{ + return grnci.NewError(grnci.ErrorCode(rc), map[string]interface{}{ "method": "C.grn_obj_close", }) } -------------- next part -------------- HTML����������������������������...Download