github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/services/shared/errors_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/authzed/grpcutil"
     9  	"github.com/stretchr/testify/require"
    10  	"google.golang.org/grpc/codes"
    11  
    12  	"github.com/authzed/spicedb/internal/dispatch"
    13  	dispatchv1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
    14  )
    15  
    16  func TestRewriteCanceledError(t *testing.T) {
    17  	ctx, cancelFunc := context.WithCancel(context.Background())
    18  	cancelFunc()
    19  	errorRewritten := RewriteError(ctx, ctx.Err(), nil)
    20  	grpcutil.RequireStatus(t, codes.Canceled, errorRewritten)
    21  }
    22  
    23  func TestRewriteDeadlineExceededError(t *testing.T) {
    24  	ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now())
    25  	defer cancelFunc()
    26  	errorRewritten := RewriteError(ctx, ctx.Err(), nil)
    27  	grpcutil.RequireStatus(t, codes.DeadlineExceeded, errorRewritten)
    28  }
    29  
    30  func TestRewriteMaximumDepthExceededError(t *testing.T) {
    31  	errorRewritten := RewriteError(context.Background(), dispatch.NewMaxDepthExceededError(nil), &ConfigForErrors{
    32  		MaximumAPIDepth: 50,
    33  	})
    34  	require.ErrorContains(t, errorRewritten, "See: https://spicedb.dev/d/debug-max-depth")
    35  	grpcutil.RequireStatus(t, codes.ResourceExhausted, errorRewritten)
    36  }
    37  
    38  func TestRewriteMaximumDepthExceededErrorForCheck(t *testing.T) {
    39  	errorRewritten := RewriteError(context.Background(), dispatch.NewMaxDepthExceededError(&dispatchv1.DispatchCheckRequest{}), &ConfigForErrors{
    40  		MaximumAPIDepth: 50,
    41  	})
    42  	require.ErrorContains(t, errorRewritten, "See: https://spicedb.dev/d/debug-max-depth-check")
    43  	require.ErrorContains(t, errorRewritten, "--explain")
    44  	grpcutil.RequireStatus(t, codes.ResourceExhausted, errorRewritten)
    45  }