fix(ws): surface HTTP status/body on relay dial failure - #134
Open
PrashantBtkl wants to merge 1 commit into
Open
fix(ws): surface HTTP status/body on relay dial failure#134PrashantBtkl wants to merge 1 commit into
PrashantBtkl wants to merge 1 commit into
Conversation
Bad-handshake errors from the relay were logged as an opaque "websocket: bad handshake" with no indication of why (relay down, auth rejected, rate limited, etc.). gorilla/websocket returns the HTTP response alongside ErrBadHandshake; capture and log its status code and body instead of discarding it.
There was a problem hiding this comment.
Code Review
This pull request enhances error reporting during WebSocket connection failures by capturing and reading the HTTP response body and status code. The reviewer identified a potential nil pointer dereference panic if resp.Body is nil and provided a code suggestion to safely check resp.Body before reading or closing it.
Comment on lines
+148
to
+152
| if resp != nil { | ||
| body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024)) | ||
| resp.Body.Close() // nolint:errcheck | ||
| return fmt.Errorf("dial failed: %w (status=%d body=%q)", err, resp.StatusCode, body) | ||
| } |
There was a problem hiding this comment.
To prevent potential nil pointer dereference panics, we should check if resp.Body is nil before attempting to read from or close it. Even if resp.Body is nil, we can still safely report the HTTP status code.
if resp != nil {
var body []byte
if resp.Body != nil {
body, _ = io.ReadAll(io.LimitReader(resp.Body, 1024))
resp.Body.Close() // nolint:errcheck
}
return fmt.Errorf("dial failed: %w (status=%d body=%q)", err, resp.StatusCode, body)
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
dial failed: websocket: bad handshakefor ~30 minutes during a relay-side connectivity issue, with no way to tell whether the relay was down, rejecting auth, or rate limiting.gorilla/websocketreturns the HTTP response alongsideErrBadHandshake, but forager discarded it (conn, _, err := ...).status=502vsstatus=401vsstatus=429instead of an opaque error.Test plan
go build ./...go vet ./pkg/ws/...go test ./pkg/ws/... ./cmd/...