errors.go 2.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package ipld

import (
	"fmt"
)

// ErrWrongKind may be returned from functions on the Node interface when
// a method is invoked which doesn't make sense for the Kind and/or ReprKind
// that node concretely contains.
//
// For example, calling AsString on a map will return ErrWrongKind.
12
// Calling LookupString on an int will similarly return ErrWrongKind.
13
type ErrWrongKind struct {
Eric Myhre's avatar
Eric Myhre committed
14 15 16 17 18 19
	// CONSIDER: if we should add a `TypeName string` here as well?
	// It seems to be useful information, and in many places we've shoved it
	// along with the MethodName; but while that's fine for the printed message,
	// we could do better internally (and it would enable `typed.wrapnode*` to
	// touch this field on its way out in a nice reusable way).

20 21 22 23
	// MethodName is literally the string for the operation attempted, e.g.
	// "AsString".
	MethodName string

24 25 26 27 28
	// ApprorpriateKind describes which ReprKinds the erroring method would
	// make sense for.
	AppropriateKind ReprKindSet

	// ActualKind describes the ReprKind of the node the method was called on.
29 30 31
	//
	// In the case of typed nodes, this will typically refer to the 'natural'
	// data-model kind for such a type (e.g., structs will say 'map' here).
32
	ActualKind ReprKind
33 34 35 36 37
}

func (e ErrWrongKind) Error() string {
	return fmt.Sprintf("func called on wrong kind: %s called on a %s node, but only makes sense on %s", e.MethodName, e.ActualKind, e.AppropriateKind)
}
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

// ErrNotExists may be returned from the traversal functions of the Node
// interface to indicate a missing value.
//
// Note that typed.ErrNoSuchField is another type of error which sometimes
// occurs in similar places as ErrNotExists.  ErrNoSuchField is preferred
// when handling data with constraints provided by a schema that mean that
// a field can *never* exist (as differentiated from a map key which is
// simply absent in some data).
type ErrNotExists struct {
	Segment string // REVIEW: might be better to use PathSegment, but depends on another refactor.
}

func (e ErrNotExists) Error() string {
	return fmt.Sprintf("key not found: %q", e.Segment)
}
54 55 56 57 58 59 60 61

// ErrIteratorOverread is returned when calling 'Next' on a MapIterator or
// ListIterator when it is already done.
type ErrIteratorOverread struct{}

func (e ErrIteratorOverread) Error() string {
	return "iterator overread"
}