Skip to content

Commit

Permalink
agent/exec: rewrite IsTemporary() to use errors.As()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
thaJeztah committed Jun 27, 2022
1 parent 6068d18 commit 37849a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
12 changes: 3 additions & 9 deletions agent/exec/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,9 @@ func (t temporary) Temporary() bool { return true }
// IsTemporary returns true if the error or a recursive cause returns true for
// temporary.
func IsTemporary(err error) bool {
if tmp, ok := err.(Temporary); ok && tmp.Temporary() {
return true
var tmp Temporary
if errors.As(err, &tmp) {
return tmp.Temporary()
}

cause := errors.Cause(err)

if tmp, ok := cause.(Temporary); ok && tmp.Temporary() {
return true
}

return false
}
27 changes: 27 additions & 0 deletions agent/exec/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package exec

import (
"fmt"
"testing"

"github.com/pkg/errors"
)

func TestIsTemporary(t *testing.T) {
err := fmt.Errorf("err")
err1 := MakeTemporary(fmt.Errorf("err1: %w", err))
err2 := fmt.Errorf("err2: %w", err1)
err3 := errors.Wrap(err2, "err3")
err4 := fmt.Errorf("err4: %w", err3)
err5 := errors.Wrap(err4, "err5")

if IsTemporary(nil) {
t.Error("expected error to not be a temporary error")
}
if IsTemporary(err) {
t.Error("expected error to not be a temporary error")
}
if !IsTemporary(err5) {
t.Error("expected error to be a temporary error")
}
}

0 comments on commit 37849a7

Please sign in to comment.