mirror of https://github.com/status-im/consul.git
Handle wrapped errors in isFailedPreconditionErr
This commit is contained in:
parent
4ed74dd513
commit
de73171202
|
@ -606,6 +606,15 @@ func isFailedPreconditionErr(err error) bool {
|
|||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Handle wrapped errors, since status.FromError does a naive assertion.
|
||||
var statusErr interface {
|
||||
GRPCStatus() *grpcstatus.Status
|
||||
}
|
||||
if errors.As(err, &statusErr) {
|
||||
return statusErr.GRPCStatus().Code() == codes.FailedPrecondition
|
||||
}
|
||||
|
||||
grpcErr, ok := grpcstatus.FromError(err)
|
||||
if !ok {
|
||||
return false
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
"github.com/armon/go-metrics"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
|
@ -1332,3 +1333,13 @@ func TestLeader_Peering_retryLoopBackoffPeering_cancelContext(t *testing.T) {
|
|||
fmt.Errorf("error 1"),
|
||||
}, allErrors)
|
||||
}
|
||||
|
||||
func Test_isFailedPreconditionErr(t *testing.T) {
|
||||
st := grpcstatus.New(codes.FailedPrecondition, "cannot establish a peering stream on a follower node")
|
||||
err := st.Err()
|
||||
assert.True(t, isFailedPreconditionErr(err))
|
||||
|
||||
// test that wrapped errors are checked correctly
|
||||
werr := fmt.Errorf("wrapped: %w", err)
|
||||
assert.True(t, isFailedPreconditionErr(werr))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue