github.com/tomwright/dasel@v1.27.3/error.go (about)

     1  package dasel
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"reflect"
     7  )
     8  
     9  // ErrMissingPreviousNode is returned when findValue doesn't have access to the previous node.
    10  var ErrMissingPreviousNode = errors.New("missing previous node")
    11  
    12  // UnknownComparisonOperatorErr is returned when
    13  type UnknownComparisonOperatorErr struct {
    14  	Operator string
    15  }
    16  
    17  // Error returns the error message.
    18  func (e UnknownComparisonOperatorErr) Error() string {
    19  	return fmt.Sprintf("unknown comparison operator: %s", e.Operator)
    20  }
    21  
    22  // Is implements the errors interface, so the errors.Is() function can be used.
    23  func (e UnknownComparisonOperatorErr) Is(err error) bool {
    24  	_, ok := err.(*UnknownComparisonOperatorErr)
    25  	return ok
    26  }
    27  
    28  // InvalidIndexErr is returned when a selector targets an index that does not exist.
    29  type InvalidIndexErr struct {
    30  	Index string
    31  }
    32  
    33  // Error returns the error message.
    34  func (e InvalidIndexErr) Error() string {
    35  	return fmt.Sprintf("invalid index: %s", e.Index)
    36  }
    37  
    38  // Is implements the errors interface, so the errors.Is() function can be used.
    39  func (e InvalidIndexErr) Is(err error) bool {
    40  	_, ok := err.(*InvalidIndexErr)
    41  	return ok
    42  }
    43  
    44  // UnsupportedSelector is returned when a specific selector type is used in the wrong context.
    45  type UnsupportedSelector struct {
    46  	Selector string
    47  }
    48  
    49  // Error returns the error message.
    50  func (e UnsupportedSelector) Error() string {
    51  	return fmt.Sprintf("selector is not supported here: %s", e.Selector)
    52  }
    53  
    54  // Is implements the errors interface, so the errors.Is() function can be used.
    55  func (e UnsupportedSelector) Is(err error) bool {
    56  	_, ok := err.(*UnsupportedSelector)
    57  	return ok
    58  }
    59  
    60  // UnsupportedTypeForSelector is returned when a selector attempts to handle a data type it can't handle.
    61  type UnsupportedTypeForSelector struct {
    62  	Selector Selector
    63  	Value    reflect.Value
    64  }
    65  
    66  // Error returns the error message.
    67  func (e UnsupportedTypeForSelector) Error() string {
    68  	return fmt.Sprintf("selector [type:%s selector:%s] does not support value: [kind:%s type:%T] %v", e.Selector.Type, e.Selector.Raw, e.Value.Kind().String(), e.Value.Interface(), e.Value.Interface())
    69  }
    70  
    71  // Is implements the errors interface, so the errors.Is() function can be used.
    72  func (e UnsupportedTypeForSelector) Is(err error) bool {
    73  	_, ok := err.(*UnsupportedTypeForSelector)
    74  	return ok
    75  }
    76  
    77  // ValueNotFound is returned when a selector string cannot be fully resolved.
    78  type ValueNotFound struct {
    79  	Selector      string
    80  	PreviousValue reflect.Value
    81  }
    82  
    83  // Error returns the error message.
    84  func (e ValueNotFound) Error() string {
    85  	return fmt.Sprintf("no value found for selector: %s: %v", e.Selector, e.PreviousValue)
    86  }
    87  
    88  // Is implements the errors interface, so the errors.Is() function can be used.
    89  func (e ValueNotFound) Is(err error) bool {
    90  	_, ok := err.(*ValueNotFound)
    91  	return ok
    92  }
    93  
    94  // UnexpectedPreviousNilValue is returned when the previous node contains a nil value.
    95  type UnexpectedPreviousNilValue struct {
    96  	Selector string
    97  }
    98  
    99  // Error returns the error message.
   100  func (e UnexpectedPreviousNilValue) Error() string {
   101  	return fmt.Sprintf("previous value is nil: %s", e.Selector)
   102  }
   103  
   104  // Is implements the errors interface, so the errors.Is() function can be used.
   105  func (e UnexpectedPreviousNilValue) Is(err error) bool {
   106  	_, ok := err.(*UnexpectedPreviousNilValue)
   107  	return ok
   108  }
   109  
   110  // UnhandledCheckType is returned when the a check doesn't know how to deal with the given type
   111  type UnhandledCheckType struct {
   112  	Value interface{}
   113  }
   114  
   115  // Error returns the error message.
   116  func (e UnhandledCheckType) Error() string {
   117  	return fmt.Sprintf("unhandled check type: %T", e.Value)
   118  }
   119  
   120  // Is implements the errors interface, so the errors.Is() function can be used.
   121  func (e UnhandledCheckType) Is(err error) bool {
   122  	_, ok := err.(*UnhandledCheckType)
   123  	return ok
   124  }