forked from cloudfoundry-attic/bosh-ext-cli
/
errs.go
78 lines (64 loc) · 1.83 KB
/
errs.go
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
74
75
76
77
78
package patch
import (
"fmt"
"sort"
"strings"
)
type opMismatchTypeErr struct {
type_ string
path Pointer
obj interface{}
}
func newOpArrayMismatchTypeErr(tokens []Token, obj interface{}) opMismatchTypeErr {
return opMismatchTypeErr{"an array", NewPointer(tokens), obj}
}
func newOpMapMismatchTypeErr(tokens []Token, obj interface{}) opMismatchTypeErr {
return opMismatchTypeErr{"a map", NewPointer(tokens), obj}
}
func (e opMismatchTypeErr) Error() string {
errMsg := "Expected to find %s at path '%s' but found '%T'"
return fmt.Sprintf(errMsg, e.type_, e.path, e.obj)
}
type opMissingMapKeyErr struct {
key string
path Pointer
obj map[interface{}]interface{}
}
func (e opMissingMapKeyErr) Error() string {
errMsg := "Expected to find a map key '%s' for path '%s' (%s)"
return fmt.Sprintf(errMsg, e.key, e.path, e.siblingKeysErrStr())
}
func (e opMissingMapKeyErr) siblingKeysErrStr() string {
if len(e.obj) == 0 {
return "found no other map keys"
}
var keys []string
for key, _ := range e.obj {
if keyStr, ok := key.(string); ok {
keys = append(keys, keyStr)
}
}
sort.Sort(sort.StringSlice(keys))
return "found map keys: '" + strings.Join(keys, "', '") + "'"
}
type OpMissingIndexErr struct {
Idx int
Obj []interface{}
}
func (e OpMissingIndexErr) Error() string {
return fmt.Sprintf("Expected to find array index '%d' but found array of length '%d'", e.Idx, len(e.Obj))
}
type opMultipleMatchingIndexErr struct {
path Pointer
idxs []int
}
func (e opMultipleMatchingIndexErr) Error() string {
return fmt.Sprintf("Expected to find exactly one matching array item for path '%s' but found %d", e.path, len(e.idxs))
}
type opUnexpectedTokenErr struct {
token Token
path Pointer
}
func (e opUnexpectedTokenErr) Error() string {
return fmt.Sprintf("Expected to not find token '%T' at path '%s'", e.token, e.path)
}