github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/spiceerrors/testutil.go (about)

     1  package spiceerrors
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"google.golang.org/genproto/googleapis/rpc/errdetails"
     8  	"google.golang.org/grpc/status"
     9  
    10  	v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
    11  )
    12  
    13  // RequireReason asserts that an error is a gRPC error and returns the expected
    14  // reason in the ErrorInfo.
    15  // TODO(jschorr): Move into grpcutil.
    16  func RequireReason(t testing.TB, reason v1.ErrorReason, err error, expectedMetadataKeys ...string) {
    17  	require.Error(t, err)
    18  	withStatus, ok := status.FromError(err)
    19  	require.True(t, ok)
    20  	require.True(t, len(withStatus.Details()) > 0)
    21  
    22  	info := withStatus.Details()[0].(*errdetails.ErrorInfo)
    23  	require.Equal(t, v1.ErrorReason_name[int32(reason)], info.GetReason())
    24  
    25  	for _, expectedKey := range expectedMetadataKeys {
    26  		require.Contains(t, info.Metadata, expectedKey)
    27  	}
    28  }