Skip to content

Commit

Permalink
log request body on 500 error to aid debugging (#2283)
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Callaway <[email protected]>
  • Loading branch information
bobcallaway authored Nov 21, 2024
1 parent 92584b7 commit 9fddf00
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pkg/api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/mitchellh/mapstructure"

"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/generated/restapi/operations/entries"
Expand Down Expand Up @@ -67,18 +66,15 @@ func handleRekorAPIError(params interface{}, code int, err error, message string
typeStr := fmt.Sprintf("%T", params)
handler := re.FindStringSubmatch(typeStr)[1]

logMsg := func(r *http.Request) {
logMsg := func(r *http.Request, inputs ...interface{}) {
ctx := r.Context()
fields := append([]interface{}{"handler", handler, "statusCode", code, "clientMessage", message}, fields...)
if code >= 500 {
fields = append(fields, inputs...)
log.ContextLogger(ctx).Errorw(err.Error(), fields...)
} else {
log.ContextLogger(ctx).Warnw(err.Error(), fields...)
}
paramsFields := map[string]interface{}{}
if err := mapstructure.Decode(params, &paramsFields); err == nil {
log.ContextLogger(ctx).Debug(paramsFields)
}
}

switch params := params.(type) {
Expand Down Expand Up @@ -119,11 +115,16 @@ func handleRekorAPIError(params interface{}, code int, err error, message string
}
return resp
default:
logMsg(params.HTTPRequest)
requestFields := []interface{}{"requestBody", params.ProposedEntry}
logMsg(params.HTTPRequest, requestFields...)
return entries.NewCreateLogEntryDefault(code).WithPayload(errorMsg(message, code))
}
case entries.SearchLogQueryParams:
logMsg(params.HTTPRequest)
requestFields := []interface{}{}
if params.Entry != nil {
requestFields = append(requestFields, "requestBody", *params.Entry)
}
logMsg(params.HTTPRequest, requestFields...)
switch code {
case http.StatusBadRequest:
return entries.NewSearchLogQueryBadRequest().WithPayload(errorMsg(message, code))
Expand All @@ -147,7 +148,11 @@ func handleRekorAPIError(params interface{}, code int, err error, message string
logMsg(params.HTTPRequest)
return pubkey.NewGetPublicKeyDefault(code).WithPayload(errorMsg(message, code))
case index.SearchIndexParams:
logMsg(params.HTTPRequest)
requestFields := []interface{}{}
if params.Query != nil {
requestFields = append(requestFields, "requestBody", *params.Query)
}
logMsg(params.HTTPRequest, requestFields...)
switch code {
case http.StatusBadRequest:
return index.NewSearchIndexBadRequest().WithPayload(errorMsg(message, code))
Expand Down

0 comments on commit 9fddf00

Please sign in to comment.