增加错误信息解析
This commit is contained in:
parent
017e4bc540
commit
84bf18c576
1
go.mod
1
go.mod
@ -9,5 +9,6 @@ require (
|
|||||||
github.com/lestrrat-go/strftime v1.0.4 // indirect
|
github.com/lestrrat-go/strftime v1.0.4 // indirect
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/spaolacci/murmur3 v1.1.0
|
github.com/spaolacci/murmur3 v1.1.0
|
||||||
|
github.com/stretchr/testify v1.7.0
|
||||||
go.uber.org/zap v1.16.0
|
go.uber.org/zap v1.16.0
|
||||||
)
|
)
|
||||||
|
5
go.sum
5
go.sum
@ -2,6 +2,7 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
|
|||||||
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
|
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
|
||||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
@ -68,13 +69,16 @@ github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7
|
|||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||||
|
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||||
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||||
@ -156,5 +160,6 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
|||||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-developer/gopkg/convert"
|
"github.com/go-developer/gopkg/convert"
|
||||||
@ -17,6 +18,30 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// defaultParseError ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 10:59 下午 2021/2/27
|
||||||
|
func defaultParseError(err error) error {
|
||||||
|
if nil == err {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
errMsg := err.Error()
|
||||||
|
if errMsg == "nil" || errMsg == "<nil>" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
strArr := strings.Split(errMsg, ":")
|
||||||
|
if len(strArr) != 2 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
msg := strings.ToLower(strings.TrimSpace(strArr[1]))
|
||||||
|
if msg == "nil" || msg == "<nil>" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Options 连接选项,百分之百兼容第三方包的选项
|
// Options 连接选项,百分之百兼容第三方包的选项
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<张德满>
|
// Author : go_developer@163.com<张德满>
|
||||||
@ -41,10 +66,14 @@ type RealClient struct {
|
|||||||
// Author : go_developer@163.com<张德满>
|
// Author : go_developer@163.com<张德满>
|
||||||
//
|
//
|
||||||
// Date : 5:05 下午 2021/2/27
|
// Date : 5:05 下午 2021/2/27
|
||||||
func NewClient(config map[string]Options) (ClientInterface, error) {
|
func NewClient(config map[string]Options, parseErrorFunc func(err error) error) (ClientInterface, error) {
|
||||||
c := &Client{
|
c := &Client{
|
||||||
instanceTable: make(map[string]*RealClient),
|
instanceTable: make(map[string]*RealClient),
|
||||||
confTable: config,
|
confTable: config,
|
||||||
|
parseErrorFunc: parseErrorFunc,
|
||||||
|
}
|
||||||
|
if nil == c.parseErrorFunc {
|
||||||
|
c.parseErrorFunc = defaultParseError
|
||||||
}
|
}
|
||||||
return c, c.init()
|
return c, c.init()
|
||||||
}
|
}
|
||||||
@ -55,8 +84,9 @@ func NewClient(config map[string]Options) (ClientInterface, error) {
|
|||||||
//
|
//
|
||||||
// Date : 4:52 下午 2021/2/27
|
// Date : 4:52 下午 2021/2/27
|
||||||
type Client struct {
|
type Client struct {
|
||||||
instanceTable map[string]*RealClient // redis 实例
|
instanceTable map[string]*RealClient // redis 实例
|
||||||
confTable map[string]Options // redis 配置
|
confTable map[string]Options // redis 配置
|
||||||
|
parseErrorFunc func(err error) error // 解析err的function,解析执行结果是否为失败,有的场景,执行成功,返回 redis:nil / redis:<nil>
|
||||||
}
|
}
|
||||||
|
|
||||||
// init 初始化redis连接
|
// init 初始化redis连接
|
||||||
@ -175,7 +205,7 @@ func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...in
|
|||||||
startTime := time.Now().Unix()
|
startTime := time.Now().Unix()
|
||||||
cmdResult := realClient.Instance.Do(ctx.Ctx, redisCmd...)
|
cmdResult := realClient.Instance.Do(ctx.Ctx, redisCmd...)
|
||||||
go c.log(ctx, realClient, cmdResult, startTime, time.Now().UnixNano())
|
go c.log(ctx, realClient, cmdResult, startTime, time.Now().UnixNano())
|
||||||
return cmdResult.Val(), cmdResult.Err()
|
return cmdResult.Val(), c.parseErrorFunc(cmdResult.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommandProxyWithReceiver 执行命令,并解析结果
|
// CommandProxyWithReceiver 执行命令,并解析结果
|
||||||
@ -199,6 +229,11 @@ func (c *Client) CommandProxyWithReceiver(ctx *Context, flag string, receiver in
|
|||||||
return ResultConvertFail(convert.ConvertAssign(receiver, result))
|
return ResultConvertFail(convert.ConvertAssign(receiver, result))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClientInterface 定义redis client的接口实现,方便单元测试数据mock
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 10:49 下午 2021/2/27
|
||||||
type ClientInterface interface {
|
type ClientInterface interface {
|
||||||
GetRedisClient(flag string) (*RealClient, error)
|
GetRedisClient(flag string) (*RealClient, error)
|
||||||
CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (interface{}, error)
|
CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (interface{}, error)
|
@ -11,6 +11,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
redisInstance "github.com/go-redis/redis/v8"
|
redisInstance "github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,9 +37,11 @@ func TestCommandProxy(t *testing.T) {
|
|||||||
},
|
},
|
||||||
LoggerFieldConfig: nil,
|
LoggerFieldConfig: nil,
|
||||||
},
|
},
|
||||||
})
|
}, nil)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
fmt.Println(instance.CommandProxy(nil, "test_redis", "set", "command_proxy", "hello world"))
|
r, cmdErr := instance.CommandProxy(nil, "test_redis", "set", "command_proxy", "hello world")
|
||||||
|
assert.Nil(t, cmdErr, "命令执行成功")
|
||||||
|
assert.Equal(t, "OK", fmt.Sprintf("%v", r))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user