mirror of
https://gitee.com/johng/gf
synced 2026-07-04 21:03:13 +08:00
change errors wrapped by gerror.Wrap with error stack info for all packages
This commit is contained in:
@ -24,7 +24,7 @@ type Adapter interface {
|
||||
|
||||
// Conn is an interface of a connection from universal redis client.
|
||||
type Conn interface {
|
||||
// Do sends a command to the server and returns the received reply.
|
||||
// Do send a command to the server and returns the received reply.
|
||||
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
|
||||
Do(ctx context.Context, command string, args ...interface{}) (result *gvar.Var, err error)
|
||||
|
||||
|
||||
@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
@ -54,8 +55,11 @@ func NewAdapterGoRedis(config *Config) *AdapterGoRedis {
|
||||
|
||||
// Close closes the redis connection pool, which will release all connections reserved by this pool.
|
||||
// It is commonly not necessary to call Close manually.
|
||||
func (r *AdapterGoRedis) Close(ctx context.Context) error {
|
||||
return r.client.Close()
|
||||
func (r *AdapterGoRedis) Close(ctx context.Context) (err error) {
|
||||
if err = r.client.Close(); err != nil {
|
||||
err = gerror.Wrap(err, `Redis Client Close failed`)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Conn retrieves and returns a connection object for continuous operations.
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
@ -21,54 +22,45 @@ type localAdapterGoRedisConn struct {
|
||||
redis *AdapterGoRedis
|
||||
}
|
||||
|
||||
// Do sends a command to the server and returns the received reply.
|
||||
// Do send a command to the server and returns the received reply.
|
||||
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
|
||||
func (c *localAdapterGoRedisConn) Do(ctx context.Context, command string, args ...interface{}) (reply *gvar.Var, err error) {
|
||||
argStrSlice := gconv.Strings(args)
|
||||
switch gstr.ToLower(command) {
|
||||
case `subscribe`:
|
||||
c.ps = c.redis.client.Subscribe(ctx, gconv.Strings(args)...)
|
||||
c.ps = c.redis.client.Subscribe(ctx, argStrSlice...)
|
||||
|
||||
case `psubscribe`:
|
||||
c.ps = c.redis.client.PSubscribe(ctx, gconv.Strings(args)...)
|
||||
c.ps = c.redis.client.PSubscribe(ctx, argStrSlice...)
|
||||
|
||||
case `unsubscribe`:
|
||||
if c.ps != nil {
|
||||
err = c.ps.Unsubscribe(ctx, gconv.Strings(args)...)
|
||||
err = c.ps.Unsubscribe(ctx, argStrSlice...)
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `Redis PubSub Unsubscribe failed with arguments "%v"`, argStrSlice)
|
||||
}
|
||||
}
|
||||
|
||||
case `punsubscribe`:
|
||||
if c.ps != nil {
|
||||
err = c.ps.PUnsubscribe(ctx, gconv.Strings(args)...)
|
||||
err = c.ps.PUnsubscribe(ctx, argStrSlice...)
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `Redis PubSub PUnsubscribe failed with arguments "%v"`, argStrSlice)
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
arguments := make([]interface{}, len(args)+1)
|
||||
copy(arguments, []interface{}{command})
|
||||
copy(arguments[1:], args)
|
||||
reply, err = c.resultToVar(
|
||||
c.redis.client.Do(ctx, arguments...).Result(),
|
||||
)
|
||||
reply, err = c.resultToVar(c.redis.client.Do(ctx, arguments...).Result())
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `Redis Client Do failed with arguments "%v"`, arguments)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Receive receives a single reply as gvar.Var from the Redis server.
|
||||
func (c *localAdapterGoRedisConn) Receive(ctx context.Context) (*gvar.Var, error) {
|
||||
if c.ps != nil {
|
||||
return c.resultToVar(c.ps.Receive(ctx))
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Close closes current PubSub or puts the connection back to connection pool.
|
||||
func (c *localAdapterGoRedisConn) Close(ctx context.Context) error {
|
||||
if c.ps != nil {
|
||||
return c.ps.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// resultToVar converts redis operation result to gvar.Var.
|
||||
func (c *localAdapterGoRedisConn) resultToVar(result interface{}, err error) (*gvar.Var, error) {
|
||||
if err == redis.Nil {
|
||||
@ -98,5 +90,29 @@ func (c *localAdapterGoRedisConn) resultToVar(result interface{}, err error) (*g
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return gvar.New(result), err
|
||||
}
|
||||
|
||||
// Receive receives a single reply as gvar.Var from the Redis server.
|
||||
func (c *localAdapterGoRedisConn) Receive(ctx context.Context) (*gvar.Var, error) {
|
||||
if c.ps != nil {
|
||||
v, err := c.resultToVar(c.ps.Receive(ctx))
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `Redis PubSub Receive failed`)
|
||||
}
|
||||
return v, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Close closes current PubSub or puts the connection back to connection pool.
|
||||
func (c *localAdapterGoRedisConn) Close(ctx context.Context) (err error) {
|
||||
if c.ps != nil {
|
||||
err = c.ps.Close()
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `Redis PubSub Close failed`)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -78,7 +78,7 @@ func SetConfigByMap(m map[string]interface{}, name ...string) error {
|
||||
func ConfigFromMap(m map[string]interface{}) (config *Config, err error) {
|
||||
config = &Config{}
|
||||
if err = gconv.Scan(m, config); err != nil {
|
||||
err = gerror.NewCodef(gcode.CodeInvalidConfiguration, `invalid redis configuration: "%+v"`, m)
|
||||
err = gerror.NewCodef(gcode.CodeInvalidConfiguration, `invalid redis configuration: %#v`, m)
|
||||
}
|
||||
if config.DialTimeout < 1000 {
|
||||
config.DialTimeout = config.DialTimeout * time.Second
|
||||
|
||||
@ -56,7 +56,7 @@ func (r *Redis) Conn(ctx context.Context) (*RedisConn, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Do sends a command to the server and returns the received reply.
|
||||
// Do send a command to the server and returns the received reply.
|
||||
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
|
||||
func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) {
|
||||
if r == nil {
|
||||
|
||||
@ -22,7 +22,7 @@ type RedisConn struct {
|
||||
redis *Redis
|
||||
}
|
||||
|
||||
// Do sends a command to the server and returns the received reply.
|
||||
// Do send a command to the server and returns the received reply.
|
||||
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
|
||||
func (c *RedisConn) Do(ctx context.Context, command string, args ...interface{}) (reply *gvar.Var, err error) {
|
||||
for k, v := range args {
|
||||
|
||||
Reference in New Issue
Block a user