Skip to content

Commit

Permalink
Fixing server error response message
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhraja committed Feb 29, 2024
1 parent cf60ec8 commit 9f821f1
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions runtime/server_http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ import (
"go.uber.org/zap/zapcore"
)

const (
// _errTmpl is Error Template
_errTmpl = `{"error":%s}`
)

// ServerHTTPResponse struct manages server http response
type ServerHTTPResponse struct {
Request *ServerHTTPRequest
Expand Down Expand Up @@ -169,7 +174,7 @@ func (res *ServerHTTPResponse) SendErrorString(
statusCode int, errMsg string,
) {
res.WriteJSONBytes(statusCode, nil,
[]byte(`{"error":"`+errMsg+`"}`),
[]byte(populateJSONTemplate(_errTmpl, errMsg)),
)
}

Expand All @@ -179,7 +184,7 @@ func (res *ServerHTTPResponse) SendError(
) {
res.Err = errCause
res.WriteJSONBytes(statusCode, nil,
[]byte(`{"error":"`+errMsg+`"}`),
[]byte(populateJSONTemplate(_errTmpl, errMsg)),
)
}

Expand Down Expand Up @@ -337,3 +342,7 @@ func (res *ServerHTTPResponse) GetPendingResponseObject() interface{} {
func (res *ServerHTTPResponse) Headers() http.Header {
return res.responseWriter.Header()
}

func populateJSONTemplate(template, msg string) string {
return fmt.Sprintf(template, strconv.Quote(msg))
}

0 comments on commit 9f821f1

Please sign in to comment.