From 76486b5cb46f7a99807456128bd155ce305ed6d8 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sat, 1 May 2021 06:47:49 -0700 Subject: [PATCH] Rename error types --- inspeq/inspector.go | 32 +++++++++++------------------- internal/rdb/inspect.go | 38 +++++++++++++++++++++++------------- internal/rdb/inspect_test.go | 2 +- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/inspeq/inspector.go b/inspeq/inspector.go index cf622bc..e300b09 100644 --- a/inspeq/inspector.go +++ b/inspeq/inspector.go @@ -5,6 +5,7 @@ package inspeq import ( + "errors" "fmt" "strconv" "strings" @@ -134,23 +135,12 @@ func (i *Inspector) History(qname string, n int) ([]*DailyStats, error) { return res, nil } -// ErrQueueNotFound indicates that the specified queue does not exist. -type ErrQueueNotFound struct { - qname string -} - -func (e *ErrQueueNotFound) Error() string { - return fmt.Sprintf("queue %q does not exist", e.qname) -} - -// ErrQueueNotEmpty indicates that the specified queue is not empty. -type ErrQueueNotEmpty struct { - qname string -} - -func (e *ErrQueueNotEmpty) Error() string { - return fmt.Sprintf("queue %q is not empty", e.qname) -} +var ( + // ErrQueueNotFound indicates that the specified queue does not exist. + ErrQueueNotFound = errors.New("queue not found") + // ErrQueueNotEmpty indicates that the specified queue is not empty. + ErrQueueNotEmpty = errors.New("queue is not empty") +) // DeleteQueue removes the specified queue. // @@ -164,11 +154,11 @@ func (e *ErrQueueNotEmpty) Error() string { // returns ErrQueueNotEmpty. func (i *Inspector) DeleteQueue(qname string, force bool) error { err := i.rdb.RemoveQueue(qname, force) - if _, ok := err.(*rdb.ErrQueueNotFound); ok { - return &ErrQueueNotFound{qname} + if _, ok := err.(*rdb.QueueNotFoundError); ok { + return fmt.Errorf("%w: queue=%q", ErrQueueNotFound, qname) } - if _, ok := err.(*rdb.ErrQueueNotEmpty); ok { - return &ErrQueueNotEmpty{qname} + if _, ok := err.(*rdb.QueueNotEmptyError); ok { + return fmt.Errorf("%w: queue=%q", ErrQueueNotEmpty, qname) } return err } diff --git a/internal/rdb/inspect.go b/internal/rdb/inspect.go index 582c3be..b4ebd75 100644 --- a/internal/rdb/inspect.go +++ b/internal/rdb/inspect.go @@ -105,7 +105,7 @@ func (r *RDB) CurrentStats(qname string) (*Stats, error) { return nil, err } if !exists { - return nil, &ErrQueueNotFound{qname} + return nil, &QueueNotFoundError{qname} } now := time.Now() res, err := currentStatsCmd.Run(r.client, []string{ @@ -219,7 +219,7 @@ func (r *RDB) HistoricalStats(qname string, n int) ([]*DailyStats, error) { return nil, err } if !exists { - return nil, &ErrQueueNotFound{qname} + return nil, &QueueNotFoundError{qname} } const day = 24 * time.Hour now := time.Now().UTC() @@ -594,6 +594,9 @@ func (r *RDB) ArchiveAllPendingTasks(qname string) (int64, error) { return n, nil } +// archiveTaskCmd is a Lua script that arhives a task given a task id. +// +// Input: // KEYS[1] -> task key (asynq:{}:t:) // KEYS[2] -> archived key (asynq:{}:archived) // ARGV[1] -> id of the task to archive @@ -601,6 +604,9 @@ func (r *RDB) ArchiveAllPendingTasks(qname string) (int64, error) { // ARGV[3] -> cutoff timestamp (e.g., 90 days ago) // ARGV[4] -> max number of tasks in archived state (e.g., 100) // ARGV[5] -> queue key prefix (asynq:{}:) +// +// Output: +// TODO: document return value of the script var archiveTaskCmd = redis.NewScript(` if redis.call("EXISTS", KEYS[1]) == 0 then return 0 @@ -826,22 +832,26 @@ func (r *RDB) DeleteAllPendingTasks(qname string) (int64, error) { return n, nil } -// ErrQueueNotFound indicates specified queue does not exist. -type ErrQueueNotFound struct { - qname string +// QueueNotFoundError indicates specified queue does not exist. +type QueueNotFoundError struct { + Name string // name of the queue } -func (e *ErrQueueNotFound) Error() string { - return fmt.Sprintf("queue %q does not exist", e.qname) +func (e *QueueNotFoundError) Unwrap() error { return base.ErrNotFound } + +func (e *QueueNotFoundError) Error() string { + return fmt.Sprintf("queue %q does not exist", e.Name) } -// ErrQueueNotEmpty indicates specified queue is not empty. -type ErrQueueNotEmpty struct { - qname string +// QueueNotEmptyError indicates specified queue is not empty. +type QueueNotEmptyError struct { + Name string // name of the queue } -func (e *ErrQueueNotEmpty) Error() string { - return fmt.Sprintf("queue %q is not empty", e.qname) +func (e *QueueNotEmptyError) Unwrap() error { return base.ErrFailedPrecondition } + +func (e *QueueNotEmptyError) Error() string { + return fmt.Sprintf("queue %q is not empty", e.Name) } // Only check whether active queue is empty before removing. @@ -931,7 +941,7 @@ func (r *RDB) RemoveQueue(qname string, force bool) error { return err } if !exists { - return &ErrQueueNotFound{qname} + return &QueueNotFoundError{qname} } var script *redis.Script if force { @@ -949,7 +959,7 @@ func (r *RDB) RemoveQueue(qname string, force bool) error { } if err := script.Run(r.client, keys, base.TaskKeyPrefix(qname)).Err(); err != nil { if err.Error() == "QUEUE NOT EMPTY" { - return &ErrQueueNotEmpty{qname} + return &QueueNotEmptyError{qname} } return err } diff --git a/internal/rdb/inspect_test.go b/internal/rdb/inspect_test.go index 8208523..f430e50 100644 --- a/internal/rdb/inspect_test.go +++ b/internal/rdb/inspect_test.go @@ -224,7 +224,7 @@ func TestCurrentStatsWithNonExistentQueue(t *testing.T) { qname := "non-existent" got, err := r.CurrentStats(qname) if err == nil { - t.Fatalf("r.CurrentStats(%q) = %v, %v, want nil, %v", qname, got, err, &ErrQueueNotFound{qname}) + t.Fatalf("r.CurrentStats(%q) = %v, %v, want nil, %v", qname, got, err, &QueueNotFoundError{qname}) } }