7 // PathError is a specialization of error that represents where in a
8 // potentially-deep data structure an error occured, using a Path.
9 type PathError struct {
14 func errorf(path Path, f string, args ...interface{}) error {
15 // We need to copy the Path because often our caller builds it by
16 // continually mutating the same underlying buffer.
17 sPath := make(Path, len(path))
20 error: fmt.Errorf(f, args...),
25 // NewErrorf creates a new PathError for the current path by passing the
26 // given format and arguments to fmt.Errorf and then wrapping the result
27 // similarly to NewError.
28 func (p Path) NewErrorf(f string, args ...interface{}) error {
29 return errorf(p, f, args...)
32 // NewError creates a new PathError for the current path, wrapping the given
34 func (p Path) NewError(err error) error {
35 // if we're being asked to wrap an existing PathError then our new
36 // PathError will be the concatenation of the two paths, ensuring
37 // that we still get a single flat PathError that's thus easier for
38 // callers to deal with.
39 perr, wrappingPath := err.(PathError)
42 pathLen = pathLen + len(perr.Path)
45 sPath := make(Path, pathLen)
48 copy(sPath[len(p):], perr.Path)