Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
pull-requests: read
jobs:
golangci:
name: lint
Expand All @@ -32,7 +32,7 @@ jobs:
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
Expand Down
25 changes: 19 additions & 6 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,14 @@ func (h *Handler) wrappedHandler(handler func(ctx context.Context, r *http.Reque

response, err = handler(ctx, r, p, contentType)
if err != nil {
if writeErr := WriteErrorResponse(w, err.Error(), response.StatusCode); writeErr != nil {
h.log.WithError(writeErr).Error("Failed to write error response")
}
h.writeError(w, registeredPath, response.StatusCode, err)

return
}

data, err := response.MarshalAs(contentType)
if err != nil {
if writeErr := WriteErrorResponse(w, err.Error(), http.StatusInternalServerError); writeErr != nil {
h.log.WithError(writeErr).Error("Failed to write error response")
}
h.writeError(w, registeredPath, http.StatusInternalServerError, err)

return
}
Expand All @@ -139,6 +135,23 @@ func (h *Handler) wrappedHandler(handler func(ctx context.Context, r *http.Reque
}
}

// writeError responds with an error, hiding internal error detail from clients
// on 5xx responses while still logging it server-side. Client errors (4xx) keep
// their message since it is actionable feedback about the request.
func (h *Handler) writeError(w http.ResponseWriter, path string, statusCode int, err error) {
msg := err.Error()

if statusCode >= http.StatusInternalServerError {
h.log.WithError(err).WithField("path", path).Error("Request failed")

msg = http.StatusText(statusCode)
}

if writeErr := WriteErrorResponse(w, msg, statusCode); writeErr != nil {
h.log.WithError(writeErr).Error("Failed to write error response")
}
}

func (h *Handler) handleEthV1BeaconGenesis(ctx context.Context, r *http.Request, p httprouter.Params, contentType ContentType) (*HTTPResponse, error) {
if err := ValidateContentType(contentType, []ContentType{ContentTypeJSON}); err != nil {
return NewUnsupportedMediaTypeResponse(nil), err
Expand Down
Loading