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

     1  package errors
     2  
     3  // ValueError describes where the error is.
     4  type ValueError interface {
     5  	error
     6  	ValueError() ValueError
     7  }
     8  
     9  type ValueErrorBuilder interface {
    10  	ValueError
    11  
    12  	// Wrap returns a copy with wrapped error is set to detailingError.
    13  	Wrap(detailingError error) ValueErrorBuilder
    14  }
    15  
    16  type valueConstantErrorDescriptor string
    17  
    18  var (
    19  	_ error                = valueConstantErrorDescriptor("")
    20  	_ ErrorDescriptor      = valueConstantErrorDescriptor("")
    21  	_ ValueErrorDescriptor = valueConstantErrorDescriptor("")
    22  )
    23  
    24  func (e valueConstantErrorDescriptor) Error() string                      { return string(e) }
    25  func (e valueConstantErrorDescriptor) ErrorDescriptorString() string      { return string(e) }
    26  func (e valueConstantErrorDescriptor) ValueErrorDescriptorString() string { return string(e) }
    27  
    28  type ValueErrorDescriptor interface {
    29  	ErrorDescriptor
    30  	ValueErrorDescriptorString() string
    31  }
    32  
    33  const (
    34  	// Value was not provided (nil)
    35  	ErrValueUnspecified = valueConstantErrorDescriptor("unspecified")
    36  	// Value was provided but empty
    37  	ErrValueEmpty = valueConstantErrorDescriptor("empty")
    38  
    39  	ErrValueMalformed = valueConstantErrorDescriptor("malformed")
    40  	// The value provided does not match the one required by the system
    41  	ErrValueMismatch = valueConstantErrorDescriptor("mismatch")
    42  	// The value provided is currently not supported or not recognized by
    43  	// the system
    44  	ErrValueUnsupported     = valueConstantErrorDescriptor("unsupported")
    45  	ErrValueTypeUnsupported = valueConstantErrorDescriptor("type unsupported")
    46  
    47  	// If everything else fails
    48  	ErrValueInvalid = valueConstantErrorDescriptor("invalid")
    49  )
    50  
    51  func ValueMalformed() ValueErrorBuilder {
    52  	return &valueDescriptorDetailsError{descriptor: ErrValueMalformed}
    53  }
    54  
    55  func IsValueMalformedError(err error) bool {
    56  	if err == ErrValueMalformed {
    57  		return true
    58  	}
    59  	if desc := UnwrapDescriptor(err); desc != nil {
    60  		return desc == ErrValueMalformed
    61  	}
    62  	return false
    63  }
    64  
    65  type valueDescriptorDetailsError struct {
    66  	descriptor valueConstantErrorDescriptor
    67  	wrapped    error
    68  }
    69  
    70  var (
    71  	_ error             = valueDescriptorDetailsError{}
    72  	_ ValueError        = valueDescriptorDetailsError{}
    73  	_ ValueErrorBuilder = valueDescriptorDetailsError{}
    74  	_ hasDescriptor     = valueDescriptorDetailsError{}
    75  	_ Unwrappable       = valueDescriptorDetailsError{}
    76  )
    77  
    78  func (e valueDescriptorDetailsError) Error() string {
    79  	if e.descriptor != "" {
    80  		if e.wrapped != nil {
    81  			return e.descriptor.Error() + ": " + e.wrapped.Error()
    82  		}
    83  		return e.descriptor.Error()
    84  	}
    85  	if e.wrapped != nil {
    86  		return e.wrapped.Error()
    87  	}
    88  	return ""
    89  }
    90  
    91  func (e valueDescriptorDetailsError) Descriptor() ErrorDescriptor { return e.descriptor }
    92  func (e valueDescriptorDetailsError) ValueError() ValueError      { return e }
    93  func (e valueDescriptorDetailsError) Unwrap() error               { return e.wrapped }
    94  
    95  func (e valueDescriptorDetailsError) Wrap(detailingError error) ValueErrorBuilder {
    96  	e.wrapped = detailingError
    97  	return e
    98  }