error.go raw
1 package backoff
2
3 import (
4 "fmt"
5 "time"
6 )
7
8 // PermanentError signals that the operation should not be retried.
9 type PermanentError struct {
10 Err error
11 }
12
13 // Permanent wraps the given err in a *PermanentError.
14 func Permanent(err error) error {
15 if err == nil {
16 return nil
17 }
18 return &PermanentError{
19 Err: err,
20 }
21 }
22
23 // Error returns a string representation of the Permanent error.
24 func (e *PermanentError) Error() string {
25 return e.Err.Error()
26 }
27
28 // Unwrap returns the wrapped error.
29 func (e *PermanentError) Unwrap() error {
30 return e.Err
31 }
32
33 // RetryAfterError signals that the operation should be retried after the given duration.
34 type RetryAfterError struct {
35 Duration time.Duration
36 }
37
38 // RetryAfter returns a RetryAfter error that specifies how long to wait before retrying.
39 func RetryAfter(seconds int) error {
40 return &RetryAfterError{Duration: time.Duration(seconds) * time.Second}
41 }
42
43 // Error returns a string representation of the RetryAfter error.
44 func (e *RetryAfterError) Error() string {
45 return fmt.Sprintf("retry after %s", e.Duration)
46 }
47