-
Notifications
You must be signed in to change notification settings - Fork 34
/
req_tsfm_bad_error.go
57 lines (44 loc) · 1.13 KB
/
req_tsfm_bad_error.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
package httptransport
import (
"net/http"
pkgerr "github.com/pkg/errors"
"github.com/machinefi/w3bstream/pkg/depends/kit/statusx"
vldterr "github.com/machinefi/w3bstream/pkg/depends/kit/validator/errors"
)
type BadRequestError interface {
EnableErrTalk()
SetMsg(msg string)
AddErr(err error, nameOrIdx ...interface{})
}
type badRequest struct {
errorFields statusx.ErrorFields
errTalk bool
msg string
}
func (e *badRequest) EnableErrTalk() { e.errTalk = true }
func (e *badRequest) SetMsg(msg string) { e.msg = msg }
func (e *badRequest) AddErr(err error, nameOrIdx ...interface{}) {
if len(nameOrIdx) > 1 {
e.errorFields = append(e.errorFields, &statusx.ErrorField{
In: nameOrIdx[0].(string),
Field: vldterr.KeyPath(nameOrIdx[1:]).String(),
Msg: err.Error(),
})
}
}
func (e *badRequest) Err() error {
if len(e.errorFields) == 0 {
return nil
}
msg := e.msg
if msg == "" {
msg = "invalid parameters"
}
err := statusx.Wrap(pkgerr.New(""), http.StatusBadRequest, "badRequest").
WithMsg(msg).
AppendErrorFields(e.errorFields...)
if e.errTalk {
err = err.EnableErrTalk()
}
return err
}