Susumu Yata
null+****@clear*****
Wed Aug 2 16:53:49 JST 2017
Susumu Yata 2017-08-02 16:53:49 +0900 (Wed, 02 Aug 2017) New Revision: ec1d87b004aaf5a307f255fe213b17e46ed27dfa https://github.com/groonga/grnci/commit/ec1d87b004aaf5a307f255fe213b17e46ed27dfa Message: Use NewError2 in libgrn. Modified files: v2/libgrn/conn.go v2/libgrn/libgrn.go v2/libgrn/response.go Modified: v2/libgrn/conn.go (+4 -11) =================================================================== --- v2/libgrn/conn.go 2017-08-02 16:47:00 +0900 (67716fd) +++ v2/libgrn/conn.go 2017-08-02 16:53:49 +0900 (de84728) @@ -106,9 +106,7 @@ func create(path string, options *connOptions) (*conn, error) { // Dup duplicates the conn if it is a DB handle. func (c *conn) Dup() (*conn, error) { if c.db == nil { - return nil, grnci.NewError(grnci.OperationError, map[string]interface{}{ - "error": "GQTP clients do not support Dup.", - }) + return nil, grnci.NewError2(grnci.OperationError, "GQTP clients do not support Dup.", nil) } ctx, err := c.db.Dup() if err != nil { @@ -242,19 +240,14 @@ func (c *conn) execDB(cmd string, body io.Reader) (grnci.Response, error) { // Exec sends a command and receives a response. func (c *conn) Exec(cmd string, body io.Reader) (grnci.Response, error) { if c.broken { - return nil, grnci.NewError(grnci.OperationError, map[string]interface{}{ - "error": "The connection is broken.", - }) + return nil, grnci.NewError2(grnci.OperationError, "The connection is broken.", nil) } if !c.ready { - return nil, grnci.NewError(grnci.OperationError, map[string]interface{}{ - "error": "The connection is not ready to send a command.", - }) + return nil, grnci.NewError2(grnci.OperationError, "The connection is not ready to send a command.", nil) } if len(cmd) > maxChunkSize { - return nil, grnci.NewError(grnci.CommandError, map[string]interface{}{ + return nil, grnci.NewError2(grnci.CommandError, "The command is too long.", map[string]interface{}{ "length": len(cmd), - "error": "The command is too long.", }) } c.ready = false Modified: v2/libgrn/libgrn.go (+11 -31) =================================================================== --- v2/libgrn/libgrn.go 2017-08-02 16:47:00 +0900 (3fe9823) +++ v2/libgrn/libgrn.go 2017-08-02 16:53:49 +0900 (50f3842) @@ -42,10 +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.ErrorCode(rc), map[string]interface{}{ - "method": "C.grn_init", - "error": "Failed to initialize libgroonga.", - }) + return grnci.NewError2(grnci.ErrorCode(rc), "C.grn_init failed.", nil) } } libCount++ @@ -63,18 +60,14 @@ func Fin() error { libMutex.Lock() defer libMutex.Unlock() if libCount <= 0 { - return grnci.NewError(grnci.OperationError, map[string]interface{}{ + return grnci.NewError2(grnci.OperationError, "libCount must be greater than 0.", map[string]interface{}{ "libCount": libCount, - "error": "libCount must be greater than 0.", }) } libCount-- if !initFinDisabled && libCount == 0 { if rc := C.grn_fin(); rc != C.GRN_SUCCESS { - return grnci.NewError(grnci.ErrorCode(rc), map[string]interface{}{ - "method": "C.grn_fin", - "error": "Failed to finalize libgroonga.", - }) + return grnci.NewError2(grnci.ErrorCode(rc), "C.grn_fin failed.", nil) } } return nil @@ -100,9 +93,7 @@ func newGrnCtx() (*grnCtx, error) { ctx := C.grn_ctx_open(C.int(0)) if ctx == nil { Fin() - return nil, grnci.NewError(grnci.UnexpectedError, map[string]interface{}{ - "method": "C.grn_ctx_open", - }) + return nil, grnci.NewError2(grnci.UnexpectedError, "C.grn_ctx_open failed.", nil) } return &grnCtx{ctx: ctx}, nil } @@ -110,9 +101,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.ErrorCode(rc), map[string]interface{}{ - "method": "C.grn_ctx_close", - }) + return grnci.NewError2(grnci.ErrorCode(rc), "C.grn_ctx_close failed.", nil) } if err := Fin(); err != nil { return err @@ -125,9 +114,7 @@ func (c *grnCtx) Err(method string) error { if c.ctx.rc == C.GRN_SUCCESS { return nil } - data := map[string]interface{}{ - "method": method, - } + data := make(map[string]interface{}) if c.ctx.errline != 0 { data["line"] = int(c.ctx.errline) } @@ -140,7 +127,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.ErrorCode(c.ctx.rc), data) + return grnci.NewError2(grnci.ErrorCode(c.ctx.rc), method+" failed.", data) } // Send sends data with flags. @@ -193,9 +180,7 @@ func createGrnDB(ctx *grnCtx, path string) (*grnDB, error) { if err := ctx.Err("C.grn_db_create"); err != nil { return nil, err } - return nil, grnci.NewError(grnci.UnexpectedError, map[string]interface{}{ - "method": "C.grn_db_create", - }) + return nil, grnci.NewError2(grnci.UnexpectedError, "C.grn_db_create failed.", nil) } return &grnDB{ obj: obj, @@ -212,9 +197,7 @@ func openGrnDB(ctx *grnCtx, path string) (*grnDB, error) { if err := ctx.Err("C.grn_db_open"); err != nil { return nil, err } - return nil, grnci.NewError(grnci.UnexpectedError, map[string]interface{}{ - "method": "C.grn_db_open", - }) + return nil, grnci.NewError2(grnci.UnexpectedError, "C.grn_db_open failed.", nil) } return &grnDB{ obj: obj, @@ -227,9 +210,8 @@ func (db *grnDB) Close(ctx *grnCtx) error { db.mutex.Lock() defer db.mutex.Unlock() if db.count <= 0 { - return grnci.NewError(grnci.OperationError, map[string]interface{}{ + return grnci.NewError2(grnci.OperationError, "count must be greater than 0.", map[string]interface{}{ "count": db.count, - "error": "count must be greater than 0.", }) } db.count-- @@ -238,9 +220,7 @@ func (db *grnDB) Close(ctx *grnCtx) error { if err := ctx.Err("C.grn_obj_close"); err != nil { return err } - return grnci.NewError(grnci.ErrorCode(rc), map[string]interface{}{ - "method": "C.grn_obj_close", - }) + return grnci.NewError2(grnci.ErrorCode(rc), "C.grn_obj_close failed.", map[string]interface{}{}) } db.obj = nil } Modified: v2/libgrn/response.go (+2 -3) =================================================================== --- v2/libgrn/response.go 2017-08-02 16:47:00 +0900 (fea1556) +++ v2/libgrn/response.go 2017-08-02 16:53:49 +0900 (b34a9ae) @@ -69,9 +69,8 @@ func (r *response) Close() error { if !r.conn.broken { if _, err = io.CopyBuffer(ioutil.Discard, r, r.conn.buf); err != nil { r.conn.broken = true - err = grnci.NewError(grnci.NetworkError, map[string]interface{}{ - "method": "io.CopyBuffer", - "error": err.Error(), + err = grnci.NewError2(grnci.NetworkError, "io.CopyBuffer failed.", map[string]interface{}{ + "error": err.Error(), }) } } -------------- next part -------------- HTML����������������������������...Download