httputils: preserve format val types

This commit is contained in:
Yousong Zhou
2020-09-18 17:22:39 +08:00
parent e805d0be45
commit d21c262c14
3 changed files with 10 additions and 27 deletions
+8 -19
View File
@@ -24,27 +24,16 @@ import (
// class: error class
// msg: message
// params: message format parameters
func NewJsonClientError(code int, class string, msg string, params ...interface{}) *JSONClientError {
details, err := errorMessage(msg, params...)
return &JSONClientError{Code: code, Class: class, Details: details, Data: err}
}
func errorMessage(msgFmt string, params ...interface{}) (string, Error) {
fields := make([]string, len(params))
for i, v := range params {
fields[i] = fmt.Sprint(v)
}
err := Error{
Id: msgFmt,
Fields: fields,
}
msg := msgFmt
func NewJsonClientError(code int, class string, msgFmt string, params ...interface{}) *JSONClientError {
details := msgFmt
if len(params) > 0 {
msg = fmt.Sprintf(msg, params...)
details = fmt.Sprintf(msgFmt, params...)
}
return msg, err
theErr := Error{
Id: msgFmt,
Fields: params,
}
return &JSONClientError{Code: code, Class: class, Details: details, Data: theErr}
}
func msgFmtToTmpl(msgFmt string) string {
-6
View File
@@ -50,12 +50,6 @@ func TestVariadic(t *testing.T) {
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
msg, _ := errorMessage(c.msg, c.params...)
if msg != c.out {
t.Errorf("want %s, got %s", c.out, msg)
}
})
t.Run(c.name+"_New", func(t *testing.T) {
err := NewJsonClientError(400, "InputParameterError", c.msg, c.params...)
if err.Details != c.out {
+2 -2
View File
@@ -70,8 +70,8 @@ var (
)
type Error struct {
Id string `json:"id,omitempty"`
Fields []string `json:"fields,omitempty"`
Id string `json:"id,omitempty"`
Fields []interface{} `json:"fields,omitempty"`
}
type JSONClientError struct {