github.com/alloyzeus/go-azfl@v0.0.0-20231220071816-9740126a2d07/errors/access.go (about)

     1  package errors
     2  
     3  // An AccessError is a specialization of ContextError.
     4  type AccessError interface {
     5  	ContextError
     6  	AccessError() AccessError
     7  }
     8  
     9  type AccessErrorBuilder interface {
    10  	AccessError
    11  
    12  	// Desc returns a copy with descriptor is set to desc.
    13  	Desc(desc ErrorDescriptor) AccessErrorBuilder
    14  
    15  	// DescMsg sets the descriptor with the provided string. For the best
    16  	// experience, descMsg should be defined as a constant so that the error
    17  	// users could use it to identify an error. For non-constant descriptor
    18  	// use the Wrap method.
    19  	DescMsg(descMsg string) AccessErrorBuilder
    20  
    21  	// Wrap returns a copy with wrapped error is set to detailingError.
    22  	Wrap(detailingError error) AccessErrorBuilder
    23  }
    24  
    25  func IsAccessError(err error) bool {
    26  	_, ok := err.(AccessError)
    27  	return ok
    28  }
    29  
    30  //TODO: should be Access(resourceIdentifiers ...string)
    31  func Access() AccessErrorBuilder {
    32  	return &accessError{}
    33  }
    34  
    35  const ErrAccessForbidden = accessErrorDescriptor("forbidden")
    36  
    37  func AccessForbidden() AccessErrorBuilder {
    38  	return Access().Desc(ErrAccessForbidden)
    39  }
    40  
    41  func IsAccessForbidden(err error) bool {
    42  	if err == ErrAccessForbidden {
    43  		return true
    44  	}
    45  	if desc := UnwrapDescriptor(err); desc != nil {
    46  		return desc == ErrAccessForbidden
    47  	}
    48  	return false
    49  }
    50  
    51  const ErrAuthorizationInvalid = constantErrorDescriptor("authorization invalid")
    52  
    53  func AuthorizationInvalid() AccessErrorBuilder {
    54  	return Access().Desc(ErrAuthorizationInvalid)
    55  }
    56  
    57  func IsAuthorizationInvalid(err error) bool {
    58  	if err == ErrAuthorizationInvalid {
    59  		return true
    60  	}
    61  	if desc := UnwrapDescriptor(err); desc != nil {
    62  		return desc == ErrAuthorizationInvalid
    63  	}
    64  	return false
    65  }
    66  
    67  type accessError struct {
    68  	descriptor ErrorDescriptor
    69  	wrapped    error
    70  }
    71  
    72  var (
    73  	_ error         = &accessError{}
    74  	_ Unwrappable   = &accessError{}
    75  	_ AccessError   = &accessError{}
    76  	_ hasDescriptor = &accessError{}
    77  )
    78  
    79  func (e *accessError) Error() string {
    80  	causeStr := errorString(e.wrapped)
    81  	if descStr := errorDescriptorString(e.descriptor); descStr != "" {
    82  		if causeStr != "" {
    83  			return "access " + descStr + ": " + causeStr
    84  		}
    85  		return "access " + descStr
    86  	}
    87  	if causeStr != "" {
    88  		return "access: " + causeStr
    89  	}
    90  	return "access error"
    91  }
    92  
    93  func (e *accessError) AccessError() AccessError    { return e }
    94  func (e *accessError) ContextError() ContextError  { return e }
    95  func (e *accessError) CallError() CallError        { return e }
    96  func (e *accessError) Descriptor() ErrorDescriptor { return e.descriptor }
    97  func (e *accessError) Unwrap() error               { return e.wrapped }
    98  
    99  func (e accessError) Desc(desc ErrorDescriptor) AccessErrorBuilder {
   100  	e.descriptor = desc
   101  	return &e
   102  }
   103  
   104  func (e accessError) DescMsg(descMsg string) AccessErrorBuilder {
   105  	e.descriptor = constantErrorDescriptor(descMsg)
   106  	return &e
   107  }
   108  
   109  func (e accessError) Wrap(detailingError error) AccessErrorBuilder {
   110  	e.wrapped = detailingError
   111  	return &e
   112  }
   113  
   114  type accessErrorDescriptor string
   115  
   116  var (
   117  	_ error           = accessErrorDescriptor("")
   118  	_ AccessError     = accessErrorDescriptor("")
   119  	_ ErrorDescriptor = accessErrorDescriptor("")
   120  )
   121  
   122  func (e accessErrorDescriptor) AccessError() AccessError      { return e }
   123  func (e accessErrorDescriptor) CallError() CallError          { return e }
   124  func (e accessErrorDescriptor) ContextError() ContextError    { return e }
   125  func (e accessErrorDescriptor) Error() string                 { return string(e) }
   126  func (e accessErrorDescriptor) ErrorDescriptorString() string { return string(e) }