Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing server error response message #931

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
Loading