package value12import "fmt"34// Error is used for reporting on a value-level error. It is the most general5// type of error for a value.6type Error struct {7Value Value8Inner error9}1011// TypeError is used for reporting on a value having an unexpected type.12type TypeError struct {13// Value which caused the error.14Value Value15Expected Type16}1718// Error returns the string form of the TypeError.19func (te TypeError) Error() string {20return fmt.Sprintf("expected %s, got %s", te.Expected, te.Value.Type())21}2223// Error returns the message of the decode error.24func (de Error) Error() string { return de.Inner.Error() }2526// MissingKeyError is used for reporting that a value is missing a key.27type MissingKeyError struct {28Value Value29Missing string30}3132// Error returns the string form of the MissingKeyError.33func (mke MissingKeyError) Error() string {34return fmt.Sprintf("key %q does not exist", mke.Missing)35}3637// ElementError is used to report on an error inside of an array.38type ElementError struct {39Value Value // The Array value40Index int // The index of the element with the issue41Inner error // The error from the element42}4344// Error returns the text of the inner error.45func (ee ElementError) Error() string { return ee.Inner.Error() }4647// FieldError is used to report on an invalid field inside an object.48type FieldError struct {49Value Value // The Object value50Field string // The field name with the issue51Inner error // The error from the field52}5354// Error returns the text of the inner error.55func (fe FieldError) Error() string { return fe.Inner.Error() }5657// ArgError is used to report on an invalid argument to a function.58type ArgError struct {59Function Value60Argument Value61Index int62Inner error63}6465// Error returns the text of the inner error.66func (ae ArgError) Error() string { return ae.Inner.Error() }6768// WalkError walks err for all value-related errors in this package.69// WalkError returns false if err is not an error from this package.70func WalkError(err error, f func(err error)) bool {71var foundOne bool7273nextError := err74for nextError != nil {75switch ne := nextError.(type) {76case Error:77f(nextError)78nextError = ne.Inner79foundOne = true80case TypeError:81f(nextError)82nextError = nil83foundOne = true84case MissingKeyError:85f(nextError)86nextError = nil87foundOne = true88case ElementError:89f(nextError)90nextError = ne.Inner91foundOne = true92case FieldError:93f(nextError)94nextError = ne.Inner95foundOne = true96case ArgError:97f(nextError)98nextError = ne.Inner99foundOne = true100default:101nextError = nil102}103}104105return foundOne106}107108109