-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
73 lines (60 loc) · 2.29 KB
/
errors.go
File metadata and controls
73 lines (60 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package protopath
import (
"errors"
"fmt"
)
// ErrAccessToNonCompositeType error returned during child access attempt on non-composite value (value of type other then message, list or map).
var ErrAccessToNonCompositeType = errors.New("connot descend into specified item; attempted to access a sub-value in an entity that is not an instance of a message, list or map")
// ErrMutationOfReadOnlyValue error returned when accessing read-only child using mutable operation.
var ErrMutationOfReadOnlyValue = errors.New("connot mutate read-only value")
// ErrNotFound error indicating that the given entity (message field, list index or map key) was not found.
type ErrNotFound struct {
Kind string // kind of not found item ("field", "index" or "key")
Value string // value that was not found
}
func (e *ErrNotFound) Error() string {
if e.Kind == "" {
return fmt.Sprintf("%q not found", e.Value)
}
return fmt.Sprintf("%s %q not found", e.Kind, e.Value)
}
// ErrInPath wrapping error that adds information about path under which some error occurred.
type ErrInPath struct {
Path string // path under which error was encountered
Cause error // underlying error
}
// NewErrInPath function that can wrap into ErrInPath the provided error and the given path. If the provided error is ErrInPath it will join all paths together.
func NewErrInPath(path string, err error) error {
if err == nil {
return nil
}
if pe, ok := err.(*ErrInPath); ok { // update path only on direct sub path error, do not use errors.As
return &ErrInPath{Path: JoinPaths(path, pe.Path), Cause: pe.Cause}
}
return &ErrInPath{Path: path, Cause: err}
}
func newErrInSegment(s Segment, err error) error {
return NewErrInPath(s.Encoded(), err)
}
func newErrInSegments(ss Segments, err error) error {
if len(ss) == 0 {
return err
}
return NewErrInPath(ss.Encode(), err)
}
func (e *ErrInPath) Error() string {
if e.Path == "" {
return e.Cause.Error()
}
return fmt.Sprintf("proto path %q: %s", e.Path, e.Cause.Error())
}
func (e *ErrInPath) Unwrap() error {
return e.Cause
}
// ErrInvalidSegment error indicating that some segment is invalid.
type ErrInvalidSegment struct {
Encoded string // encoded value of the segment
}
func (e *ErrInvalidSegment) Error() string {
return fmt.Sprintf("proto path segment %q is invalid", e.Encoded)
}