Skip to content
This repository was archived by the owner on Dec 23, 2025. It is now read-only.

Commit e70a540

Browse files
committed
chore(binding): Optimize error format
Change-Id: Iafcf66fb5d2882cfe747b31e00d0fae68b2ce191
1 parent 64798f5 commit e70a540

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

binding/bind.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package binding
22

33
import (
4-
"errors"
54
"net/http"
65
"reflect"
76
_ "unsafe"
@@ -123,7 +122,7 @@ func (b *Binding) getObjOrPrepare(value reflect.Value) (*receiver, error) {
123122
return false
124123
}
125124

126-
p := recv.getOrAddParam(fh)
125+
p := recv.getOrAddParam(fh, b.bindErrFactory)
127126
in := auto
128127
name := fh.FieldSelector().Name()
129128

@@ -176,17 +175,14 @@ func (b *Binding) getObjOrPrepare(value reflect.Value) (*receiver, error) {
176175
}
177176
p.in = in
178177
p.name = name
179-
p.requiredError = errors.New("missing required parameter: " + name)
180-
p.typeError = errors.New("parameter type does not match binding data: " + name)
181-
p.cannotError = errors.New("parameter cannot be bound: " + name)
182178
return true
183179
})
184180

185181
if errMsg != "" {
186182
return nil, b.bindErrFactory(errExprSelector.String(), errMsg)
187183
}
188184

189-
recv.combNamePath()
185+
recv.initParams()
190186

191187
b.recvs.Store(runtimeTypeID, recv)
192188
return recv, nil

binding/param_info.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ type paramInfo struct {
2020
name string
2121
required bool
2222

23-
requiredError, typeError, cannotError error
23+
bindErrFactory func(failField, msg string) error
24+
25+
requiredError, typeError, cannotError, contentTypeError error
2426
}
2527

2628
func (p *paramInfo) getField(expr *tagexpr.TagExpr) (reflect.Value, error) {
@@ -106,7 +108,7 @@ func (p *paramInfo) bindBody(expr *tagexpr.TagExpr, bodyCodec uint8, postForm ur
106108
case jsonBody:
107109
return p.bindJSON(expr, bodyBytes)
108110
}
109-
return false, nil
111+
return false, p.contentTypeError
110112
}
111113

112114
func (p *paramInfo) bindJSON(expr *tagexpr.TagExpr, bodyBytes []byte) (bool, error) {
@@ -236,7 +238,3 @@ func (p *paramInfo) setStringSlice(v reflect.Value, a []string) error {
236238

237239
return p.typeError
238240
}
239-
240-
// func (p *paramInfo) newError(errStr string) error {
241-
// return errors.New("field type does not match binding data: " + p.fieldSelector)
242-
// }

binding/receiver.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (r *receiver) getParam(fieldSelector string) *paramInfo {
3939
return nil
4040
}
4141

42-
func (r *receiver) getOrAddParam(fh *tagexpr.FieldHandler) *paramInfo {
42+
func (r *receiver) getOrAddParam(fh *tagexpr.FieldHandler, bindErrFactory func(failField, msg string) error) *paramInfo {
4343
fieldSelector := fh.StringSelector()
4444
p := r.getParam(fieldSelector)
4545
if p != nil {
@@ -48,6 +48,7 @@ func (r *receiver) getOrAddParam(fh *tagexpr.FieldHandler) *paramInfo {
4848
p = new(paramInfo)
4949
p.fieldSelector = fieldSelector
5050
p.structField = fh.StructField()
51+
p.bindErrFactory = bindErrFactory
5152
r.params = append(r.params, p)
5253
return p
5354
}
@@ -103,15 +104,7 @@ func (r *receiver) getCookies(req *http.Request) []*http.Cookie {
103104
return nil
104105
}
105106

106-
// func (a *receiver) getPath(req *http.Request) *url.Values {
107-
// v := new(url.Values)
108-
// if a.hasQuery {
109-
// (*v) = req.URL.Query()
110-
// }
111-
// return v
112-
// }
113-
114-
func (r *receiver) combNamePath() {
107+
func (r *receiver) initParams() {
115108
if !r.hasBody {
116109
return
117110
}
@@ -136,5 +129,9 @@ func (r *receiver) combNamePath() {
136129
}
137130
}
138131
p.namePath = namePath + p.name
132+
p.requiredError = p.bindErrFactory(p.namePath, "missing required parameter")
133+
p.typeError = p.bindErrFactory(p.namePath, "parameter type does not match binding data")
134+
p.cannotError = p.bindErrFactory(p.namePath, "parameter cannot be bound")
135+
p.contentTypeError = p.bindErrFactory(p.namePath, "does not support binding to the content type body")
139136
}
140137
}

0 commit comments

Comments
 (0)