-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
improve error handling in writePacket #1601
Conversation
* handle error before success case. * return io.ErrShortWrite if not all bytes were written but err is nil. * return err instead of ErrInvalidConn.
WalkthroughThe recent changes enhance error handling and configuration initialization in the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- packets.go (1 hunks)
Additional comments not posted (2)
packets.go (2)
127-137
: Improved error handling inwritePacket
function.The changes here handle the case where an error occurs during packet writing more robustly. Specifically, it checks if an error was returned and no bytes were written, then returns
errBadConnNoWrite
. This is a good practice as it ensures that the error state of the connection is accurately reported, preventing further operations on a potentially bad connection.
140-145
: Ensure proper error handling when packet size written is incorrect.The addition of a check for
io.ErrShortWrite
when the number of bytes written does not match the expected size is a crucial improvement. This adheres to theio.Writer
interface contract, which states that a non-nil error should be returned if the full length of bytes is not written. This change ensures that such protocol violations are correctly handled, improving the robustness of the packet writing logic.
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- connection_test.go (2 hunks)
Additional comments not posted (2)
connection_test.go (2)
166-167
: Initialization ofclosech
andcfg
in themc
struct enhances the robustness and configurability of the connection handling.
189-190
: Changing the error comparison tonc.err
inTestPingErrInvalidConn
makes the test more specific and accurate.Verification successful
The review comment is correct. The
nc.err
is properly defined and assigned within theconnection_test.go
file, making the test more specific and accurate.
nc := badConnection{err: errors.New("boom")}
nc := badConnection{err: errors.New("failed to write"), n: 10}
These assignments ensure that
nc.err
is set correctly for the error comparison in the test.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the error handling in `TestPingErrInvalidConn` function. # Test: Search for error handling changes. Expect: Only the new error handling logic. rg --type go 'nc.err' connection_test.goLength of output: 110
Script:
#!/bin/bash # Description: Verify the definition and assignment of `nc.err` in connection_test.go. # Search for the definition and assignment of `nc.err` in the file. rg --type go 'nc\.err' connection_test.goLength of output: 113
Script:
#!/bin/bash # Description: Locate the definition and assignment of `nc` in connection_test.go. # Search for the definition and assignment of `nc` in the file. rg --type go '\bnc\b' connection_test.goLength of output: 340
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.
LGTM
Checklist
Summary by CodeRabbit
Bug Fixes
Tests
TestPingErrInvalidConn
for more accurate test results.