-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding DelayedError * minor fix * langage optimization
- Loading branch information
1 parent
3f26d94
commit f5f6739
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package errorutil | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"time" | ||
) | ||
|
||
// Delayer defines an error that | ||
type Delayer interface { | ||
Delay() time.Duration | ||
} | ||
|
||
// Delay return the delay duration of a DelayedError (i.e. implements Delayer). | ||
// If the error is nil or does not implement Delayer or the delay is not a positive value, 0 is returned. | ||
func Delay(err error) time.Duration { | ||
type causer interface { | ||
Cause() error | ||
} | ||
|
||
for err != nil { | ||
if delay, ok := err.(Delayer); ok { | ||
return delay.Delay() | ||
} | ||
cause, ok := err.(causer) | ||
if !ok { | ||
break | ||
} | ||
err = cause.Cause() | ||
} | ||
return 0 | ||
} | ||
|
||
// DelayedError set error delay duration. returns nil if the error is nil. | ||
func WithDelay(err error, duration time.Duration) error { | ||
if err == nil { | ||
return nil | ||
} | ||
return &delayedError{error: err, duration: duration} | ||
} | ||
|
||
// NewDelayedError returns a delayed error that formats as the given text and duration. | ||
func NewDelayedError(text string, duration time.Duration) error { | ||
return WithDelay(errors.New(text), duration) | ||
} | ||
|
||
type delayedError struct { | ||
error | ||
duration time.Duration | ||
} | ||
|
||
|
||
func (err *delayedError) Delay() time.Duration { | ||
return err.duration | ||
} | ||
|
||
func (err *delayedError) Cause() error { | ||
return err.error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package errorutil | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDelay(t *testing.T) { | ||
|
||
// err := errorutil.NewRetryableError("Delayed Error Test") | ||
tests := []struct { | ||
err error | ||
want time.Duration | ||
}{ | ||
{nil, 0}, | ||
{WithDelay(nil, 0), 0}, | ||
{WithDelay(nil, 10), 0}, | ||
{WithDelay(errors.New("foo"), 0), 0}, | ||
{WithDelay(errors.New("foo"), 10), 10}, | ||
} | ||
err := WithDelay(errors.New("foobar"), 10) | ||
if err.Error() != "foobar" { | ||
t.Errorf("WithDelay(%q): got: %v, want %v", err, err.Error(), "foobar") | ||
} | ||
for _, tt := range tests { | ||
got := Delay(tt.err) | ||
if got != tt.want { | ||
t.Errorf("WithDelay(%q): got: %v, want %v", tt.err, got, tt.want) | ||
} | ||
} | ||
} | ||
|
||
func TestNewDelayedError(t *testing.T) { | ||
err := NewDelayedError("delayed Error", 10) | ||
if err.Error() != "delayed Error" { | ||
t.Errorf("NewDelayedError expected %s, got %s", "delayed Error", err.Error()) | ||
} | ||
} |