-
Notifications
You must be signed in to change notification settings - Fork 0
fix: Catch all errors from endpoints #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sanbotto
wants to merge
9
commits into
main
Choose a base branch
from
fix/catch-all-errors-from-endpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
75f6b88
fix: Catch all errors from endpoints
sanbotto 8a182c1
Remove unnecessary code
sanbotto 4d9e026
Apply CodeRabbit's suggestions
sanbotto 39b50c1
Read the response body only when actually needed
sanbotto a01a791
Update workflow
sanbotto 9d54701
Further improvements to the workflow
sanbotto 1aa8415
Refactor error handling for 400 Bad Request
sanbotto 3d9cf4d
Remove unused fx
sanbotto 32b3da9
Simplify response body reading
sanbotto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: project-aethermesh/aetherlay
Length of output: 102
🏁 Script executed:
sed -n '570,590p' internal/server/server.goRepository: project-aethermesh/aetherlay
Length of output: 690
🏁 Script executed:
sed -n '1175,1235p' internal/server/server.goRepository: project-aethermesh/aetherlay
Length of output: 2855
🏁 Script executed:
sed -n '617,670p' internal/server/server.goRepository: project-aethermesh/aetherlay
Length of output: 2536
🏁 Script executed:
sed -n '720,740p' internal/server/server.goRepository: project-aethermesh/aetherlay
Length of output: 1451
🌐 Web query:
Go http.ResponseWriter after websocket.Upgrader.Upgrade - is connection hijacked, can you call WriteHeader or Write after upgrade?💡 Result:
Short answer: Yes — Upgrade hijacks the underlying connection, so you must not call ResponseWriter.WriteHeader or Write after Upgrade; use the returned *websocket.Conn instead. If you do call Write/WriteHeader after the hijack you’ll get errors like "http: response.Write on hijacked connection". Also note hijack is only supported for HTTP/1.x (HTTP/2 ResponseWriters don’t implement Hijacker). [1][2]
Sources:
🌐 Web query:
gorilla/websocket Dial error - do I need to close resp.Body when Dial fails?💡 Result:
No — you do not need to close resp.Body when gorilla/websocket Dial returns an error. The Dialer returns a non-nil *http.Response on handshake failure for caller inspection, but the response body may be incomplete and “does not need to be closed by the application.” [1][2]
Sources:
🏁 Script executed:
sed -n '560,580p' internal/server/server.goRepository: project-aethermesh/aetherlay
Length of output: 877
🏁 Script executed:
sed -n '1170,1185p' internal/server/server.goRepository: project-aethermesh/aetherlay
Length of output: 617
🏁 Script executed:
Repository: project-aethermesh/aetherlay
Length of output: 1515
🏁 Script executed:
Repository: project-aethermesh/aetherlay
Length of output: 1670
WebSocket handshake error handling: Upgrade happens too early in the retry loop, making HTTP response writes invalid.
The WS path attempts to mirror HTTP 400 logic, but there's a critical architectural issue:
Premature Upgrade prevents HTTP error responses in the retry loop.
defaultProxyWebSocketupgrades the client connection immediately viaupgrader.Upgrade(w, r, nil)(Line 1178). The connection is hijacked andhttp.ResponseWriterbecomes invalid for further HTTP operations.handleRequestWScallsdefaultProxyWebSocketon each retry iteration (lines 576+). If the backend handshake fails with a 400 error on the first endpoint, the function returns aBadRequestErrortohandleRequestWS.handleRequestWSthen attempts to write HTTP headers and body viaw.Header().Add,w.WriteHeader, andw.Write(lines 638–641) to pass through the error to the client. But since the connection was already hijacked by the Upgrade call in the previousdefaultProxyWebSocketinvocation, these writes will fail with "response.Write on hijacked connection" errors.Upgrade. Only callUpgradeonce you confirm the backend will accept the connection, or restructure sodefaultProxyWebSocketdoes not upgrade on retry attempts that fail.Response body closure on handshake error (lines 1203–1207) is not required.
resp.Bodydoes not need to be closed by the application whenDialreturns an error. The current code reads the body without closing it, which is acceptable per the library's contract and does not cause resource leaks.Given the hijack issue, the WS 400 pass-through path will not work as written. Consider redesigning the flow to probe or perform the backend handshake before upgrading the client connection.
🤖 Prompt for AI Agents