github.com/ipld/go-ipld-prime@v0.21.0/datamodel/errors.go (about)

     1  package datamodel
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // ErrWrongKind may be returned from functions on the Node interface when
     8  // a method is invoked which doesn't make sense for the Kind that node
     9  // concretely contains.
    10  //
    11  // For example, calling AsString on a map will return ErrWrongKind.
    12  // Calling Lookup on an int will similarly return ErrWrongKind.
    13  type ErrWrongKind struct {
    14  	// TypeName may optionally indicate the named type of a node the function
    15  	// was called on (if the node was typed!), or, may be the empty string.
    16  	TypeName string
    17  
    18  	// MethodName is literally the string for the operation attempted, e.g.
    19  	// "AsString".
    20  	//
    21  	// For methods on nodebuilders, we say e.g. "NodeBuilder.CreateMap".
    22  	MethodName string
    23  
    24  	// ApprorpriateKind describes which Kinds the erroring method would
    25  	// make sense for.
    26  	AppropriateKind KindSet
    27  
    28  	// ActualKind describes the Kind of the node the method was called on.
    29  	//
    30  	// In the case of typed nodes, this will typically refer to the 'natural'
    31  	// data-model kind for such a type (e.g., structs will say 'map' here).
    32  	ActualKind Kind
    33  
    34  	// TODO: it may be desirable for this error to be able to describe the schema typekind, too, if applicable.
    35  	// Of course this presents certain package import graph problems.  Solution to this that maximizes user usability is unclear.
    36  }
    37  
    38  func (e ErrWrongKind) Error() string {
    39  	if e.TypeName == "" {
    40  		return fmt.Sprintf("func called on wrong kind: %q called on a %s node, but only makes sense on %s", e.MethodName, e.ActualKind, e.AppropriateKind)
    41  	} else {
    42  		return fmt.Sprintf("func called on wrong kind: %q called on a %s node (kind: %s), but only makes sense on %s", e.MethodName, e.TypeName, e.ActualKind, e.AppropriateKind)
    43  	}
    44  }
    45  
    46  // TODO: revisit the claim below about ErrNoSuchField.  I think we moved back away from that, or want to.
    47  
    48  // ErrNotExists may be returned from the lookup functions of the Node interface
    49  // to indicate a missing value.
    50  //
    51  // Note that schema.ErrNoSuchField is another type of error which sometimes
    52  // occurs in similar places as ErrNotExists.  ErrNoSuchField is preferred
    53  // when handling data with constraints provided by a schema that mean that
    54  // a field can *never* exist (as differentiated from a map key which is
    55  // simply absent in some data).
    56  type ErrNotExists struct {
    57  	Segment PathSegment
    58  }
    59  
    60  func (e ErrNotExists) Error() string {
    61  	return fmt.Sprintf("key not found: %q", e.Segment)
    62  }
    63  
    64  // ErrRepeatedMapKey is an error indicating that a key was inserted
    65  // into a map that already contains that key.
    66  //
    67  // This error may be returned by any methods that add data to a map --
    68  // any of the methods on a NodeAssembler that was yielded by MapAssembler.AssignKey(),
    69  // or from the MapAssembler.AssignDirectly() method.
    70  type ErrRepeatedMapKey struct {
    71  	Key Node
    72  }
    73  
    74  func (e ErrRepeatedMapKey) Error() string {
    75  	return fmt.Sprintf("cannot repeat map key %q", e.Key)
    76  }
    77  
    78  // ErrInvalidSegmentForList is returned when using Node.LookupBySegment and the
    79  // given PathSegment can't be applied to a list because it's unparsable as a number.
    80  type ErrInvalidSegmentForList struct {
    81  	// TypeName may indicate the named type of a node the function was called on,
    82  	// or be empty string if working on untyped data.
    83  	TypeName string
    84  
    85  	// TroubleSegment is the segment we couldn't use.
    86  	TroubleSegment PathSegment
    87  
    88  	// Reason may explain more about why the PathSegment couldn't be used;
    89  	// in practice, it's probably a 'strconv.NumError'.
    90  	Reason error
    91  }
    92  
    93  func (e ErrInvalidSegmentForList) Error() string {
    94  	v := "invalid segment for lookup on a list"
    95  	if e.TypeName != "" {
    96  		v += " of type " + e.TypeName
    97  	}
    98  	return v + fmt.Sprintf(": %q: %s", e.TroubleSegment.s, e.Reason)
    99  }
   100  
   101  // ErrIteratorOverread is returned when calling 'Next' on a MapIterator or
   102  // ListIterator when it is already done.
   103  type ErrIteratorOverread struct{}
   104  
   105  func (e ErrIteratorOverread) Error() string {
   106  	return "iterator overread"
   107  }