Skip to content

Commit 906c57a

Browse files
author
dmitriy kalinin
committed
extract some common errs
1 parent 2296e32 commit 906c57a

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

patch/errs.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,30 @@ func (e opMissingMapKeyErr) siblingKeysErrStr() string {
4949
sort.Sort(sort.StringSlice(keys))
5050
return "found map keys: '" + strings.Join(keys, "', '") + "'"
5151
}
52+
53+
type opMissingIndexErr struct {
54+
idx int
55+
obj []interface{}
56+
}
57+
58+
func (e opMissingIndexErr) Error() string {
59+
return fmt.Sprintf("Expected to find array index '%d' but found array of length '%d'", e.idx, len(e.obj))
60+
}
61+
62+
type opMultipleMatchingIndexErr struct {
63+
path Pointer
64+
idxs []int
65+
}
66+
67+
func (e opMultipleMatchingIndexErr) Error() string {
68+
return fmt.Sprintf("Expected to find exactly one matching array item for path '%s' but found %d", e.path, len(e.idxs))
69+
}
70+
71+
type opUnexpectedTokenErr struct {
72+
token Token
73+
path Pointer
74+
}
75+
76+
func (e opUnexpectedTokenErr) Error() string {
77+
return fmt.Sprintf("Expected to not find token '%T' at path '%s'", e.token, e.path)
78+
}

patch/find_op.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ func (op FindOp) Apply(doc interface{}) (interface{}, error) {
3030
}
3131

3232
if idx >= len(typedObj) {
33-
errMsg := "Expected to find array index '%d' but found array of length '%d'"
34-
return nil, fmt.Errorf(errMsg, idx, len(typedObj))
33+
return nil, opMissingIndexErr{idx, typedObj}
3534
}
3635

3736
if isLast {
@@ -69,8 +68,7 @@ func (op FindOp) Apply(doc interface{}) (interface{}, error) {
6968
}
7069
} else {
7170
if len(idxs) != 1 {
72-
errMsg := "Expected to find exactly one matching array item for path '%s' but found %d"
73-
return nil, fmt.Errorf(errMsg, NewPointer(tokens[:i+2]), len(idxs))
71+
return nil, opMultipleMatchingIndexErr{NewPointer(tokens[:i+2]), idxs}
7472
}
7573

7674
idx := idxs[0]
@@ -113,7 +111,7 @@ func (op FindOp) Apply(doc interface{}) (interface{}, error) {
113111
}
114112

115113
default:
116-
return nil, fmt.Errorf("Expected to not find token '%T' at '%s'", token, NewPointer(tokens[:i+2]))
114+
return nil, opUnexpectedTokenErr{token, NewPointer(tokens[:i+2])}
117115
}
118116
}
119117

patch/remove_op.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ func (op RemoveOp) Apply(doc interface{}) (interface{}, error) {
3131
}
3232

3333
if idx >= len(typedObj) {
34-
errMsg := "Expected to find array index '%d' but found array of length '%d'"
35-
return nil, fmt.Errorf(errMsg, idx, len(typedObj))
34+
return nil, opMissingIndexErr{idx, typedObj}
3635
}
3736

3837
if isLast {
@@ -67,8 +66,7 @@ func (op RemoveOp) Apply(doc interface{}) (interface{}, error) {
6766
}
6867

6968
if len(idxs) != 1 {
70-
errMsg := "Expected to find exactly one matching array item for path '%s' but found %d"
71-
return nil, fmt.Errorf(errMsg, NewPointer(tokens[:i+2]), len(idxs))
69+
return nil, opMultipleMatchingIndexErr{NewPointer(tokens[:i+2]), idxs}
7270
}
7371

7472
idx := idxs[0]
@@ -107,7 +105,7 @@ func (op RemoveOp) Apply(doc interface{}) (interface{}, error) {
107105
}
108106

109107
default:
110-
return nil, fmt.Errorf("Expected to not find token '%T' at '%s'", token, NewPointer(tokens[:i+2]))
108+
return nil, opUnexpectedTokenErr{token, NewPointer(tokens[:i+2])}
111109
}
112110
}
113111

patch/remove_op_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ var _ = Describe("RemoveOp.Apply", func() {
7777
_, err := RemoveOp{Path: MustNewPointerFromString("/-")}.Apply([]interface{}{})
7878
Expect(err).To(HaveOccurred())
7979
Expect(err.Error()).To(Equal(
80-
"Expected to not find token 'patch.AfterLastIndexToken' at '/-'"))
80+
"Expected to not find token 'patch.AfterLastIndexToken' at path '/-'"))
8181
})
8282

8383
Describe("array item with matching key and value", func() {

patch/replace_op.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ func (op ReplaceOp) Apply(doc interface{}) (interface{}, error) {
3232
}
3333

3434
if idx >= len(typedObj) {
35-
errMsg := "Expected to find array index '%d' but found array of length '%d'"
36-
return nil, fmt.Errorf(errMsg, idx, len(typedObj))
35+
return nil, opMissingIndexErr{idx, typedObj}
3736
}
3837

3938
if isLast {
@@ -78,8 +77,7 @@ func (op ReplaceOp) Apply(doc interface{}) (interface{}, error) {
7877
// no need to change prevUpdate since matching item can only be a map
7978
} else {
8079
if len(idxs) != 1 {
81-
errMsg := "Expected to find exactly one matching array item for path '%s' but found %d"
82-
return nil, fmt.Errorf(errMsg, NewPointer(tokens[:i+2]), len(idxs))
80+
return nil, opMultipleMatchingIndexErr{NewPointer(tokens[:i+2]), idxs}
8381
}
8482

8583
idx := idxs[0]
@@ -129,7 +127,7 @@ func (op ReplaceOp) Apply(doc interface{}) (interface{}, error) {
129127
}
130128

131129
default:
132-
return nil, fmt.Errorf("Expected to not find token '%T' at '%s'", token, NewPointer(tokens[:i+2]))
130+
return nil, opUnexpectedTokenErr{token, NewPointer(tokens[:i+2])}
133131
}
134132
}
135133

0 commit comments

Comments
 (0)