Skip to content

Commit 1a83f79

Browse files
authored
Improve error messages from JSONCheckOff (#480)
Hopefully this will make the errors from `JSONCheckOff` and friends a bit more consistent and comprehensible.
1 parent c04aeb6 commit 1a83f79

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

internal/match/json.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func jsonCheckOffInternal(wantKey string, wantItems []interface{}, allowUnwanted
9292
return func(body []byte) error {
9393
res := gjson.GetBytes(body, wantKey)
9494
if !res.Exists() {
95-
return fmt.Errorf("missing key '%s'", wantKey)
95+
return fmt.Errorf("JSONCheckOff: missing key '%s'", wantKey)
9696
}
9797
if !res.IsArray() && !res.IsObject() {
9898
return fmt.Errorf("JSONCheckOff: key '%s' is not an array or object", wantKey)
@@ -106,7 +106,7 @@ func jsonCheckOffInternal(wantKey string, wantItems []interface{}, allowUnwanted
106106
// convert it to something we can check off
107107
item := mapper(itemRes)
108108
if item == nil {
109-
err = fmt.Errorf("JSONCheckOff: mapper function mapped %v to nil", itemRes.Raw)
109+
err = fmt.Errorf("JSONCheckOff(%s): mapper function mapped %v to nil", wantKey, itemRes.Raw)
110110
return false
111111
}
112112

@@ -119,7 +119,7 @@ func jsonCheckOffInternal(wantKey string, wantItems []interface{}, allowUnwanted
119119
}
120120
}
121121
if !allowUnwantedItems && want == -1 {
122-
err = fmt.Errorf("JSONCheckOff: unexpected item %s", item)
122+
err = fmt.Errorf("JSONCheckOff(%s): unexpected item %v (mapped value %v)", wantKey, itemRes.Raw, item)
123123
return false
124124
}
125125

@@ -132,6 +132,7 @@ func jsonCheckOffInternal(wantKey string, wantItems []interface{}, allowUnwanted
132132
if fn != nil {
133133
err = fn(item, val)
134134
if err != nil {
135+
err = fmt.Errorf("JSONCheckOff(%s): item %v failed checks: %w", wantKey, val, err)
135136
return false
136137
}
137138
}
@@ -141,7 +142,7 @@ func jsonCheckOffInternal(wantKey string, wantItems []interface{}, allowUnwanted
141142
// at this point we should have gone through all of wantItems.
142143
// If we haven't then we expected to see some items but didn't.
143144
if err == nil && len(wantItems) > 0 {
144-
err = fmt.Errorf("JSONCheckOff: did not see items: %v", wantItems)
145+
err = fmt.Errorf("JSONCheckOff(%s): did not see items: %v", wantKey, wantItems)
145146
}
146147

147148
return err

0 commit comments

Comments
 (0)