2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 00:02:19 +08:00

Add errors.RedisCommandError type

This commit is contained in:
Ken Hibino 2021-05-09 07:15:09 -07:00
parent d2d4029aba
commit ffe9aa74b3
2 changed files with 21 additions and 3 deletions

View File

@ -213,6 +213,24 @@ func IsTaskAlreadyArchived(err error) bool {
return As(err, &target) return As(err, &target)
} }
// RedisCommandError indicates that the given redis command returned error.
type RedisCommandError struct {
Command string // redis command (e.g. LRANGE, ZADD, etc)
Err error // underlying error
}
func (e *RedisCommandError) Error() string {
return fmt.Sprintf("redis command error: %s failed: %v", strings.ToUpper(e.Command), e.Err)
}
func (e *RedisCommandError) Unwrap() error { return e.Err }
// IsRedisCommandError reports whether any error in err's chain is of type RedisCommandError.
func IsRedisCommandError(err error) bool {
var target *RedisCommandError
return As(err, &target)
}
/************************************************* /*************************************************
Standard Library errors package functions Standard Library errors package functions
*************************************************/ *************************************************/

View File

@ -183,7 +183,7 @@ func (r *RDB) memoryUsage(qname string) (int64, error) {
for { for {
data, cursor, err = r.client.Scan(cursor, fmt.Sprintf("asynq:{%s}*", qname), 100).Result() data, cursor, err = r.client.Scan(cursor, fmt.Sprintf("asynq:{%s}*", qname), 100).Result()
if err != nil { if err != nil {
return 0, errors.E(op, errors.Unknown, fmt.Sprintf("redis command error: SCAN failed: %v", err)) return 0, errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "scan", Err: err})
} }
keys = append(keys, data...) keys = append(keys, data...)
if cursor == 0 { if cursor == 0 {
@ -194,7 +194,7 @@ func (r *RDB) memoryUsage(qname string) (int64, error) {
for _, k := range keys { for _, k := range keys {
n, err := r.client.MemoryUsage(k).Result() n, err := r.client.MemoryUsage(k).Result()
if err != nil { if err != nil {
return 0, errors.E(op, errors.Unknown, fmt.Sprintf("redis command error: MEMORY USAGE failed: %v", err)) return 0, errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "memory usage", Err: err})
} }
usg += n usg += n
} }
@ -220,7 +220,7 @@ func (r *RDB) HistoricalStats(qname string, n int) ([]*DailyStats, error) {
} }
exists, err := r.client.SIsMember(base.AllQueues, qname).Result() exists, err := r.client.SIsMember(base.AllQueues, qname).Result()
if err != nil { if err != nil {
return nil, errors.E(op, errors.Unknown, fmt.Sprintf("redis command error: SISMEMBER failed: %v", err)) return nil, errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sismember", Err: err})
} }
if !exists { if !exists {
return nil, errors.E(op, errors.NotFound, &errors.QueueNotFoundError{Queue: qname}) return nil, errors.E(op, errors.NotFound, &errors.QueueNotFoundError{Queue: qname})