Skip to content

Commit 0f4916e

Browse files
authored
Use %w formatting directives when fmt.Error'ing an error. (#71)
Signed-off-by: Cléo Rebert <[email protected]>
1 parent 409f622 commit 0f4916e

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

bindparam.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ func BindStyledParameterWithOptions(style string, paramName string, value string
8383
// since prior to this refactoring, they always query unescaped.
8484
value, err = url.QueryUnescape(value)
8585
if err != nil {
86-
return fmt.Errorf("error unescaping query parameter '%s': %v", paramName, err)
86+
return fmt.Errorf("error unescaping query parameter '%s': %w", paramName, err)
8787
}
8888
case ParamLocationPath:
8989
value, err = url.PathUnescape(value)
9090
if err != nil {
91-
return fmt.Errorf("error unescaping path parameter '%s': %v", paramName, err)
91+
return fmt.Errorf("error unescaping path parameter '%s': %w", paramName, err)
9292
}
9393
default:
9494
// Headers and cookies aren't escaped.
@@ -97,7 +97,7 @@ func BindStyledParameterWithOptions(style string, paramName string, value string
9797
// If the destination implements encoding.TextUnmarshaler we use it for binding
9898
if tu, ok := dest.(encoding.TextUnmarshaler); ok {
9999
if err := tu.UnmarshalText([]byte(value)); err != nil {
100-
return fmt.Errorf("error unmarshaling '%s' text as %T: %s", value, dest, err)
100+
return fmt.Errorf("error unmarshaling '%s' text as %T: %w", value, dest, err)
101101
}
102102

103103
return nil
@@ -124,7 +124,7 @@ func BindStyledParameterWithOptions(style string, paramName string, value string
124124
// Chop up the parameter into parts based on its style
125125
parts, err := splitStyledParameter(style, opts.Explode, false, paramName, value)
126126
if err != nil {
127-
return fmt.Errorf("error splitting input '%s' into parts: %s", value, err)
127+
return fmt.Errorf("error splitting input '%s' into parts: %w", value, err)
128128
}
129129

130130
return bindSplitPartsToDestinationArray(parts, dest)
@@ -287,7 +287,7 @@ func bindSplitPartsToDestinationStruct(paramName string, parts []string, explode
287287
jsonParam := "{" + strings.Join(fields, ",") + "}"
288288
err := json.Unmarshal([]byte(jsonParam), dest)
289289
if err != nil {
290-
return fmt.Errorf("error binding parameter %s fields: %s", paramName, err)
290+
return fmt.Errorf("error binding parameter %s fields: %w", paramName, err)
291291
}
292292
return nil
293293
}

bindstring.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func BindStringToObject(src string, dst interface{}) error {
100100
case reflect.Array:
101101
if tu, ok := dst.(encoding.TextUnmarshaler); ok {
102102
if err := tu.UnmarshalText([]byte(src)); err != nil {
103-
return fmt.Errorf("error unmarshaling '%s' text as %T: %s", src, dst, err)
103+
return fmt.Errorf("error unmarshaling '%s' text as %T: %w", src, dst, err)
104104
}
105105

106106
return nil
@@ -122,7 +122,7 @@ func BindStringToObject(src string, dst interface{}) error {
122122
if err != nil {
123123
parsedTime, err = time.Parse(types.DateFormat, src)
124124
if err != nil {
125-
return fmt.Errorf("error parsing '%s' as RFC3339 or 2006-01-02 time: %s", src, err)
125+
return fmt.Errorf("error parsing '%s' as RFC3339 or 2006-01-02 time: %w", src, err)
126126
}
127127
}
128128
// So, assigning this gets a little fun. We have a value to the
@@ -145,7 +145,7 @@ func BindStringToObject(src string, dst interface{}) error {
145145
}
146146
parsedTime, err := time.Parse(types.DateFormat, src)
147147
if err != nil {
148-
return fmt.Errorf("error parsing '%s' as date: %s", src, err)
148+
return fmt.Errorf("error parsing '%s' as date: %w", src, err)
149149
}
150150
parsedDate := types.Date{Time: parsedTime}
151151

deepobject.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
258258
// TODO: why is this marked as an ineffassign?
259259
tm, err = time.Parse(types.DateFormat, pathValues.value) //nolint:ineffassign,staticcheck
260260
if err != nil {
261-
return fmt.Errorf("error parsing '%s' as RFC3339 or 2006-01-02 time: %s", pathValues.value, err)
261+
return fmt.Errorf("error parsing '%s' as RFC3339 or 2006-01-02 time: %w", pathValues.value, err)
262262
}
263263
return fmt.Errorf("invalid date format: %w", err)
264264
}

styleparam.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func StyleParamWithLocation(style string, explode bool, paramName string, paramL
8080
if !convertableToTime && !convertableToDate {
8181
b, err := tu.MarshalText()
8282
if err != nil {
83-
return "", fmt.Errorf("error marshaling '%s' as text: %s", value, err)
83+
return "", fmt.Errorf("error marshaling '%s' as text: %w", value, err)
8484
}
8585

8686
return stylePrimitive(style, explode, paramName, paramLocation, string(b))
@@ -166,7 +166,7 @@ func styleSlice(style string, explode bool, paramName string, paramLocation Para
166166
part = escapeParameterString(part, paramLocation)
167167
parts[i] = part
168168
if err != nil {
169-
return "", fmt.Errorf("error formatting '%s': %s", paramName, err)
169+
return "", fmt.Errorf("error formatting '%s': %w", paramName, err)
170170
}
171171
}
172172
return prefix + strings.Join(parts, separator), nil
@@ -274,7 +274,7 @@ func styleStruct(style string, explode bool, paramName string, paramLocation Par
274274
}
275275
str, err := primitiveToString(f.Interface())
276276
if err != nil {
277-
return "", fmt.Errorf("error formatting '%s': %s", paramName, err)
277+
return "", fmt.Errorf("error formatting '%s': %w", paramName, err)
278278
}
279279
fieldDict[fieldName] = str
280280
}
@@ -295,7 +295,7 @@ func styleMap(style string, explode bool, paramName string, paramLocation ParamL
295295
for _, fieldName := range v.MapKeys() {
296296
str, err := primitiveToString(v.MapIndex(fieldName).Interface())
297297
if err != nil {
298-
return "", fmt.Errorf("error formatting '%s': %s", paramName, err)
298+
return "", fmt.Errorf("error formatting '%s': %w", paramName, err)
299299
}
300300
fieldDict[fieldName.String()] = str
301301
}

0 commit comments

Comments
 (0)