Skip to content

Commit

Permalink
httputils: preserve format val types
Browse files Browse the repository at this point in the history
  • Loading branch information
yousong committed Sep 21, 2020
1 parent 93e5f8e commit 4fb4567
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
25 changes: 7 additions & 18 deletions pkg/util/httputils/error.go
Expand Up @@ -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)
func NewJsonClientError(code int, class string, msgFmt string, params ...interface{}) *JSONClientError {
details := msgFmt
if len(params) > 0 {
details = fmt.Sprintf(msgFmt, params...)
}

err := Error{
theErr := Error{
Id: msgFmt,
Fields: fields,
}

msg := msgFmt
if len(params) > 0 {
msg = fmt.Sprintf(msg, params...)
Fields: params,
}
return msg, err
return &JSONClientError{Code: code, Class: class, Details: details, Data: theErr}
}

func msgFmtToTmpl(msgFmt string) string {
Expand Down
6 changes: 0 additions & 6 deletions pkg/util/httputils/error_test.go
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/httputils/httputils.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 4fb4567

Please sign in to comment.