github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/namespace/errors.go (about)

     1  package namespace
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/rs/zerolog"
     8  
     9  	"github.com/authzed/spicedb/internal/sharederrors"
    10  )
    11  
    12  // ErrNamespaceNotFound occurs when a namespace was not found.
    13  type ErrNamespaceNotFound struct {
    14  	error
    15  	namespaceName string
    16  }
    17  
    18  // NotFoundNamespaceName is the name of the namespace not found.
    19  func (err ErrNamespaceNotFound) NotFoundNamespaceName() string {
    20  	return err.namespaceName
    21  }
    22  
    23  // MarshalZerologObject implements zerolog object marshalling.
    24  func (err ErrNamespaceNotFound) MarshalZerologObject(e *zerolog.Event) {
    25  	e.Err(err.error).Str("namespace", err.namespaceName)
    26  }
    27  
    28  // DetailsMetadata returns the metadata for details for this error.
    29  func (err ErrNamespaceNotFound) DetailsMetadata() map[string]string {
    30  	return map[string]string{
    31  		"definition_name": err.namespaceName,
    32  	}
    33  }
    34  
    35  // ErrRelationNotFound occurs when a relation was not found under a namespace.
    36  type ErrRelationNotFound struct {
    37  	error
    38  	namespaceName string
    39  	relationName  string
    40  }
    41  
    42  // NamespaceName returns the name of the namespace in which the relation was not found.
    43  func (err ErrRelationNotFound) NamespaceName() string {
    44  	return err.namespaceName
    45  }
    46  
    47  // NotFoundRelationName returns the name of the relation not found.
    48  func (err ErrRelationNotFound) NotFoundRelationName() string {
    49  	return err.relationName
    50  }
    51  
    52  func (err ErrRelationNotFound) MarshalZerologObject(e *zerolog.Event) {
    53  	e.Err(err.error).Str("namespace", err.namespaceName).Str("relation", err.relationName)
    54  }
    55  
    56  // DetailsMetadata returns the metadata for details for this error.
    57  func (err ErrRelationNotFound) DetailsMetadata() map[string]string {
    58  	return map[string]string{
    59  		"definition_name":             err.namespaceName,
    60  		"relation_or_permission_name": err.relationName,
    61  	}
    62  }
    63  
    64  // ErrDuplicateRelation occurs when a duplicate relation was found inside a namespace.
    65  type ErrDuplicateRelation struct {
    66  	error
    67  	namespaceName string
    68  	relationName  string
    69  }
    70  
    71  // MarshalZerologObject implements zerolog object marshalling.
    72  func (err ErrDuplicateRelation) MarshalZerologObject(e *zerolog.Event) {
    73  	e.Err(err.error).Str("namespace", err.namespaceName).Str("relation", err.relationName)
    74  }
    75  
    76  // DetailsMetadata returns the metadata for details for this error.
    77  func (err ErrDuplicateRelation) DetailsMetadata() map[string]string {
    78  	return map[string]string{
    79  		"definition_name":             err.namespaceName,
    80  		"relation_or_permission_name": err.relationName,
    81  	}
    82  }
    83  
    84  // ErrPermissionsCycle occurs when a cycle exists within permissions.
    85  type ErrPermissionsCycle struct {
    86  	error
    87  	namespaceName   string
    88  	permissionNames []string
    89  }
    90  
    91  // MarshalZerologObject implements zerolog object marshalling.
    92  func (err ErrPermissionsCycle) MarshalZerologObject(e *zerolog.Event) {
    93  	e.Err(err.error).Str("namespace", err.namespaceName).Str("permissions", strings.Join(err.permissionNames, ", "))
    94  }
    95  
    96  // DetailsMetadata returns the metadata for details for this error.
    97  func (err ErrPermissionsCycle) DetailsMetadata() map[string]string {
    98  	return map[string]string{
    99  		"definition_name":  err.namespaceName,
   100  		"permission_names": strings.Join(err.permissionNames, ","),
   101  	}
   102  }
   103  
   104  // ErrUnusedCaveatParameter indicates that a caveat parameter is unused in the caveat expression.
   105  type ErrUnusedCaveatParameter struct {
   106  	error
   107  	caveatName string
   108  	paramName  string
   109  }
   110  
   111  // MarshalZerologObject implements zerolog object marshalling.
   112  func (err ErrUnusedCaveatParameter) MarshalZerologObject(e *zerolog.Event) {
   113  	e.Err(err.error).Str("caveat", err.caveatName).Str("param", err.paramName)
   114  }
   115  
   116  // DetailsMetadata returns the metadata for details for this error.
   117  func (err ErrUnusedCaveatParameter) DetailsMetadata() map[string]string {
   118  	return map[string]string{
   119  		"caveat_name":    err.caveatName,
   120  		"parameter_name": err.paramName,
   121  	}
   122  }
   123  
   124  // NewNamespaceNotFoundErr constructs a new namespace not found error.
   125  func NewNamespaceNotFoundErr(nsName string) error {
   126  	return ErrNamespaceNotFound{
   127  		error:         fmt.Errorf("object definition `%s` not found", nsName),
   128  		namespaceName: nsName,
   129  	}
   130  }
   131  
   132  // NewRelationNotFoundErr constructs a new relation not found error.
   133  func NewRelationNotFoundErr(nsName string, relationName string) error {
   134  	return ErrRelationNotFound{
   135  		error:         fmt.Errorf("relation/permission `%s` not found under definition `%s`", relationName, nsName),
   136  		namespaceName: nsName,
   137  		relationName:  relationName,
   138  	}
   139  }
   140  
   141  // NewDuplicateRelationError constructs an error indicating that a relation was defined more than once in a namespace.
   142  func NewDuplicateRelationError(nsName string, relationName string) error {
   143  	return ErrDuplicateRelation{
   144  		error:         fmt.Errorf("found duplicate relation/permission name `%s` under definition `%s`", relationName, nsName),
   145  		namespaceName: nsName,
   146  		relationName:  relationName,
   147  	}
   148  }
   149  
   150  // NewPermissionsCycleErr constructs an error indicating that a cycle exists amongst permissions.
   151  func NewPermissionsCycleErr(nsName string, permissionNames []string) error {
   152  	return ErrPermissionsCycle{
   153  		error:           fmt.Errorf("under definition `%s`, there exists a cycle in permissions: %s", nsName, strings.Join(permissionNames, ", ")),
   154  		namespaceName:   nsName,
   155  		permissionNames: permissionNames,
   156  	}
   157  }
   158  
   159  // NewUnusedCaveatParameterErr constructs indicating that a parameter was unused in a caveat expression.
   160  func NewUnusedCaveatParameterErr(caveatName string, paramName string) error {
   161  	return ErrUnusedCaveatParameter{
   162  		error:      fmt.Errorf("parameter `%s` for caveat `%s` is unused", paramName, caveatName),
   163  		caveatName: caveatName,
   164  		paramName:  paramName,
   165  	}
   166  }
   167  
   168  var (
   169  	_ sharederrors.UnknownNamespaceError = ErrNamespaceNotFound{}
   170  	_ sharederrors.UnknownRelationError  = ErrRelationNotFound{}
   171  )