mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
httputils: preserve format val types
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user