Skip to content

Commit

Permalink
Add testing helper func (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferai authored Nov 13, 2023
1 parent 89c54d5 commit d7b6db5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions temperror/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/hashicorp/go-secure-stdlib/temperror

go 1.19

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions temperror/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 13 additions & 0 deletions temperror/temperror.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ func New(inner error) tempError {
func (t tempError) Temporary() bool {
return true
}

// IsTempError returns whether it is a temporary error to avoid having to use it
// as it is in the gRPC source code, e.g.:
//
// if ne, ok := err.(interface{ Temporary() bool }); !ok || !ne.Temporary() {
//
// This function does that for you :-)
func IsTempError(err error) bool {
if ne, ok := err.(interface{ Temporary() bool }); !ok || !ne.Temporary() {
return false
}
return true
}
34 changes: 34 additions & 0 deletions temperror/temperror_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package temperror

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTempError(t *testing.T) {
tests := []struct {
name string
expectTemp bool
input error
}{
{
name: "temp-error",
input: New(errors.New("this is a temporary error")),
expectTemp: true,
},
{
name: "not-temp-error",
input: errors.New("this is not a temporary error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expectTemp, IsTempError(tt.input))
})
}
}

0 comments on commit d7b6db5

Please sign in to comment.