fix(libhttp): capitalize http error responses for better display [EE-6698] (#11109)

pull/11238/head
Matt Hook 9 months ago committed by GitHub
parent a8938779bf
commit 704d75596d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -4,6 +4,7 @@ package error
import ( import (
"errors" "errors"
"net/http" "net/http"
"unicode"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/segmentio/encoding/json" "github.com/segmentio/encoding/json"
@ -26,9 +27,19 @@ func (handler LoggerHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
} }
func capitalize(s string) string {
if len(s) == 0 {
return s
}
// Capitalize the first letter of the word or sentence
firstLetter := unicode.ToUpper(rune(s[0]))
return string(firstLetter) + s[1:]
}
func writeErrorResponse(rw http.ResponseWriter, err *HandlerError) { func writeErrorResponse(rw http.ResponseWriter, err *HandlerError) {
if err.Err == nil { if err.Err == nil {
err.Err = errors.New(err.Message) err.Err = errors.New(capitalize(err.Message))
} }
log.Debug().CallerSkipFrame(2).Err(err.Err).Int("status_code", err.StatusCode).Str("msg", err.Message).Msg("HTTP error") log.Debug().CallerSkipFrame(2).Err(err.Err).Int("status_code", err.StatusCode).Str("msg", err.Message).Msg("HTTP error")
@ -40,7 +51,7 @@ func writeErrorResponse(rw http.ResponseWriter, err *HandlerError) {
enc.SetSortMapKeys(false) enc.SetSortMapKeys(false)
enc.SetAppendNewline(false) enc.SetAppendNewline(false)
enc.Encode(&errorResponse{Message: err.Message, Details: err.Err.Error()}) enc.Encode(&errorResponse{Message: err.Message, Details: capitalize(err.Err.Error())})
} }
// WriteError is a convenience function that creates a new HandlerError before calling writeErrorResponse. // WriteError is a convenience function that creates a new HandlerError before calling writeErrorResponse.

Loading…
Cancel
Save