github.com/openfga/openfga@v1.5.4-rc1/pkg/storage/errors.go (about)

     1  package storage
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	openfgav1 "github.com/openfga/api/proto/openfga/v1"
     8  
     9  	"github.com/openfga/openfga/pkg/tuple"
    10  )
    11  
    12  var (
    13  	// ErrCollision is returned when an item already exists within the store.
    14  	ErrCollision = errors.New("item already exists")
    15  
    16  	// ErrInvalidContinuationToken is returned when the continuation token is invalid.
    17  	ErrInvalidContinuationToken = errors.New("invalid continuation token")
    18  
    19  	// ErrMismatchObjectType is returned when there is a type discrepancy between the requested
    20  	// object in the ReadChanges API and the type indicated by the continuation token.
    21  	ErrMismatchObjectType = errors.New("mismatched types in request and continuation token")
    22  
    23  	// ErrInvalidWriteInput is returned when the tuple to be written
    24  	// already existed or the tuple to be deleted did not exist.
    25  	ErrInvalidWriteInput = errors.New("invalid write input")
    26  
    27  	// ErrTransactionalWriteFailed is returned when two writes attempt to write the same tuple at the same time.
    28  	ErrTransactionalWriteFailed = errors.New("transactional write failed due to conflict")
    29  
    30  	// ErrExceededWriteBatchLimit is returned when MaxTuplesPerWrite is exceeded.
    31  	ErrExceededWriteBatchLimit = errors.New("number of operations exceeded write batch limit")
    32  
    33  	// ErrCancelled is returned when the request has been cancelled.
    34  	ErrCancelled = errors.New("request has been cancelled")
    35  
    36  	// ErrDeadlineExceeded is returned when the request's deadline is exceeded.
    37  	ErrDeadlineExceeded = errors.New("request deadline exceeded")
    38  
    39  	// ErrNotFound is returned when the object does not exist.
    40  	ErrNotFound = errors.New("not found")
    41  )
    42  
    43  // ExceededMaxTypeDefinitionsLimitError constructs an error indicating that
    44  // the maximum allowed limit for type definitions has been exceeded.
    45  func ExceededMaxTypeDefinitionsLimitError(limit int) error {
    46  	return fmt.Errorf("exceeded number of allowed type definitions: %d", limit)
    47  }
    48  
    49  // InvalidWriteInputError generates an error for invalid operations in a tuple store.
    50  // This function is invoked when an attempt is made to write or delete a tuple with invalid conditions.
    51  // Specifically, it addresses two scenarios:
    52  // 1. Attempting to delete a non-existent tuple.
    53  // 2. Attempting to write a tuple that already exists.
    54  func InvalidWriteInputError(tk tuple.TupleWithoutCondition, operation openfgav1.TupleOperation) error {
    55  	switch operation {
    56  	case openfgav1.TupleOperation_TUPLE_OPERATION_DELETE:
    57  		return fmt.Errorf(
    58  			"cannot delete a tuple which does not exist: user: '%s', relation: '%s', object: '%s': %w",
    59  			tk.GetUser(),
    60  			tk.GetRelation(),
    61  			tk.GetObject(),
    62  			ErrInvalidWriteInput,
    63  		)
    64  	case openfgav1.TupleOperation_TUPLE_OPERATION_WRITE:
    65  		return fmt.Errorf(
    66  			"cannot write a tuple which already exists: user: '%s', relation: '%s', object: '%s': %w",
    67  			tk.GetUser(),
    68  			tk.GetRelation(),
    69  			tk.GetObject(),
    70  			ErrInvalidWriteInput,
    71  		)
    72  	default:
    73  		return nil
    74  	}
    75  }