Susumu Yata
null+****@clear*****
Tue Jul 11 14:15:25 JST 2017
Susumu Yata 2017-07-11 14:15:25 +0900 (Tue, 11 Jul 2017) New Revision: a1bb99974a5bb99e15d5b560d1109b94ca350f78 https://github.com/groonga/grnci/commit/a1bb99974a5bb99e15d5b560d1109b94ca350f78 Message: Add NewGroongaError to simplify error handling. Modified files: v2/error.go v2/gqtp.go v2/http.go v2/libgrn/libgrn.go Modified: v2/error.go (+14 -0) =================================================================== --- v2/error.go 2017-07-11 13:57:13 +0900 (9fef5fa) +++ v2/error.go 2017-07-11 14:15:25 +0900 (b1c3075) @@ -266,6 +266,20 @@ 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/gqtp.go (+1 -3) =================================================================== --- v2/gqtp.go 2017-07-11 13:57:13 +0900 (c6c16b2) +++ v2/gqtp.go 2017-07-11 14:15:25 +0900 (5f68f6a) @@ -70,9 +70,7 @@ func newGQTPResponse(conn *GQTPConn, head gqtpHeader, start time.Time, name stri } if head.Status > 32767 { rc := int(head.Status) - 65536 - resp.err = NewError(GroongaError, map[string]interface{}{ - "rc": ResultCode(rc), - }) + resp.err = NewGroongaError(ResultCode(rc), nil) } return resp } Modified: v2/http.go (+1 -3) =================================================================== --- v2/http.go 2017-07-11 13:57:13 +0900 (c704303) +++ v2/http.go 2017-07-11 14:15:25 +0900 (b4b691c) @@ -85,9 +85,7 @@ Loop: // parseHTTPResponseHeaderError parses the error information in the HTTP resonse header. func parseHTTPResponseHeaderError(code int, elems []interface{}) error { - err := NewError(GroongaError, map[string]interface{}{ - "rc": ResultCode(code), - }) + err := NewGroongaError(ResultCode(code), nil) if len(elems) >= 1 { err = EnhanceError(err, map[string]interface{}{ "message": elems[0], Modified: v2/libgrn/libgrn.go (+5 -10) =================================================================== --- v2/libgrn/libgrn.go 2017-07-11 13:57:13 +0900 (d7d1e5e) +++ v2/libgrn/libgrn.go 2017-07-11 14:15:25 +0900 (5946da3) @@ -42,8 +42,7 @@ func Init() error { defer libMutex.Unlock() if !initFinDisabled && libCount == 0 { if rc := C.grn_init(); rc != C.GRN_SUCCESS { - return grnci.NewError(grnci.GroongaError, map[string]interface{}{ - "rc": grnci.ResultCode(rc), + return grnci.NewGroongaError(grnci.ResultCode(rc), map[string]interface{}{ "method": "C.grn_init", "error": "Failed to initialize libgroonga.", }) @@ -72,8 +71,7 @@ func Fin() error { libCount-- if !initFinDisabled && libCount == 0 { if rc := C.grn_fin(); rc != C.GRN_SUCCESS { - return grnci.NewError(grnci.GroongaError, map[string]interface{}{ - "rc": grnci.ResultCode(rc), + return grnci.NewGroongaError(grnci.ResultCode(rc), map[string]interface{}{ "method": "C.grn_fin", "error": "Failed to finalize libgroonga.", }) @@ -112,8 +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.NewError(grnci.GroongaError, map[string]interface{}{ - "rc": grnci.ResultCode(rc), + return grnci.NewGroongaError(grnci.ResultCode(rc), map[string]interface{}{ "method": "C.grn_ctx_close", }) } @@ -129,7 +126,6 @@ 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 { @@ -144,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.NewError(grnci.GroongaError, data) + return grnci.NewGroongaError(grnci.ResultCode(c.ctx.rc), data) } // Send sends data with flags. @@ -244,8 +240,7 @@ func (db *grnDB) Close(ctx *grnCtx) error { "rc": int(rc), }) } - return grnci.NewError(grnci.GroongaError, map[string]interface{}{ - "rc": grnci.ResultCode(rc), + return grnci.NewGroongaError(grnci.ResultCode(rc), map[string]interface{}{ "method": "C.grn_obj_close", }) } -------------- next part -------------- HTML����������������������������...Download