From 9f821f1bd501c8dceaa29955bcebe2a867ff50b3 Mon Sep 17 00:00:00 2001 From: Anirudh Raja Date: Thu, 29 Feb 2024 18:42:01 +0530 Subject: [PATCH] Fixing server error response message --- runtime/server_http_response.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/runtime/server_http_response.go b/runtime/server_http_response.go index bfd2f2e19..b538700cc 100644 --- a/runtime/server_http_response.go +++ b/runtime/server_http_response.go @@ -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 @@ -169,7 +174,7 @@ func (res *ServerHTTPResponse) SendErrorString( statusCode int, errMsg string, ) { res.WriteJSONBytes(statusCode, nil, - []byte(`{"error":"`+errMsg+`"}`), + []byte(populateJSONTemplate(_errTmpl, errMsg)), ) } @@ -179,7 +184,7 @@ func (res *ServerHTTPResponse) SendError( ) { res.Err = errCause res.WriteJSONBytes(statusCode, nil, - []byte(`{"error":"`+errMsg+`"}`), + []byte(populateJSONTemplate(_errTmpl, errMsg)), ) } @@ -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)) +}