Skip to content

Commit 37849a7

Browse files
committed
agent/exec: rewrite IsTemporary() to use errors.As()
This makes sure we detect temporary errors both with pkg/errors.Wrap() and with native Go error wrapping. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 6068d18 commit 37849a7

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

agent/exec/errors.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,9 @@ func (t temporary) Temporary() bool { return true }
7070
// IsTemporary returns true if the error or a recursive cause returns true for
7171
// temporary.
7272
func IsTemporary(err error) bool {
73-
if tmp, ok := err.(Temporary); ok && tmp.Temporary() {
74-
return true
73+
var tmp Temporary
74+
if errors.As(err, &tmp) {
75+
return tmp.Temporary()
7576
}
76-
77-
cause := errors.Cause(err)
78-
79-
if tmp, ok := cause.(Temporary); ok && tmp.Temporary() {
80-
return true
81-
}
82-
8377
return false
8478
}

agent/exec/errors_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package exec
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/pkg/errors"
8+
)
9+
10+
func TestIsTemporary(t *testing.T) {
11+
err := fmt.Errorf("err")
12+
err1 := MakeTemporary(fmt.Errorf("err1: %w", err))
13+
err2 := fmt.Errorf("err2: %w", err1)
14+
err3 := errors.Wrap(err2, "err3")
15+
err4 := fmt.Errorf("err4: %w", err3)
16+
err5 := errors.Wrap(err4, "err5")
17+
18+
if IsTemporary(nil) {
19+
t.Error("expected error to not be a temporary error")
20+
}
21+
if IsTemporary(err) {
22+
t.Error("expected error to not be a temporary error")
23+
}
24+
if !IsTemporary(err5) {
25+
t.Error("expected error to be a temporary error")
26+
}
27+
}

0 commit comments

Comments
 (0)