Skip to content

Add error handling for HTTP response writes#21

Open
alienx5499 wants to merge 2 commits intodrand:masterfrom
alienx5499:fix/http-write-error-handling
Open

Add error handling for HTTP response writes#21
alienx5499 wants to merge 2 commits intodrand:masterfrom
alienx5499:fix/http-write-error-handling

Conversation

@alienx5499
Copy link
Contributor

Problem

Multiple HTTP handlers call w.Write() without checking the returned error. Write failures are silently ignored, making it difficult to debug network issues or client disconnections.

Affected handlers:

  • writeBeacon(), GetChains(), GetHealth(), GetBeaconIds(), GetInfoV1(), GetInfoV2() in routes_handler.go
  • DisplayRoutes() in routes.go
  • /chanz handler in metrics.go

Solution

Added error checking and logging for all w.Write() calls. Since HTTP headers may have already been sent when a write error occurs, we log the error but do not attempt to return an HTTP error status.

if _, err := w.Write(json); err != nil {
    slog.Error("failed to write response", "error", err)
}

Changes

  • Added error handling for w.Write() in all HTTP handlers
  • Error logs include context about which handler encountered the failure

Testing

  • All existing tests pass
  • Code compiles successfully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant