-
Couldn't load subscription status.
- Fork 189
[194] Change return True/False pattern to try/catch pattern #923
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
base: main
Are you sure you want to change the base?
Conversation
- Update multiaddr dependency from 3ea7f866fda9268ee92506edf9d8e975274bf941 to db8124e2321f316d3b7d2733c7df11d6ad9c03e6 - This fixes the build failure caused by missing py-cid branch 'acul71/new-setup' - Resolves Read the Docs build failure in PR libp2p#923
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.
@SuchitraSwain You can take a look at other methods which is still using the True / False return value and change it to responce / exception.
| except HostException: | ||
| raise | ||
| except Exception as e: | ||
| raise HostException(f"Failed to set stream handler: {e}") from e |
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.
@SuchitraSwain Thank you for adding the try-catch block and input validation to this function. However, I believe this PR may not be addressing the core issue described in #194.
Issue #194 is specifically about "changing return True/False pattern to try/catch pattern," but the True/False pattern has already been removed from py-libp2p in previous commits.
For reference, the earlier implementation at this commit did use the True/False return pattern, but the current codebase has already migrated away from this pattern.
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.
@SuchitraSwain
Change the PR title
|
@SuchitraSwain Have you tried running all the tests locally? Are they all passing? |
YES I have tested it locally, everything passes |
|
@SuchitraSwain resolve the merge conflicts. |
|
@seetadev kindly run the CI/CD pipeline. |
|
@sumanjeet0012 : Ran the CI/Cd pipeline. Waiting for test results. |
|
@SuchitraSwain : 2 test cases are failing. Please collaborate with @sumanjeet0012 and @acul71 to get them fixed and arrive at a good conclusion on this PR. |
|
@seetadev could you please re-run the ci/cd again |
|
@seetadev The PR is ready |
|
@SuchitraSwain : Thank you so much Suchitra for considering our feedback points. Appreciate it. Will do a final review today. Please add a test suite and a newsfragment file too. CCing @sumanjeet0012, who will help you complete these aspects in the PR. |
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.
@SuchitraSwain The PR looks good to me and is ready to go from my side. I have just left a minor piece of feedback for your consideration.
Once it is addressed, we will run the CI/CD pipeline and proceed with merging this PR.
|
Great work team 👏 @SuchitraSwain — thank you for taking the time to carefully rework the return pattern into a proper try/catch exception handling flow, add test coverage, and follow through on the review feedback. This brings much-needed consistency across the codebase and improves clarity for future maintainers. @sumanjeet0012 — thanks for the thorough review, guidance, and ensuring the changes aligned with the original issue intent. The back-and-forth here really helped refine the PR into a solid contribution. With the added tests and newsfragment in place, this looks ready to land 🚀 @sumanjeet0012 : Will like you do a final review and add a thumbs up to this comment. Will merge right away. Appreciate the collaboration from both sides! |
|
@SuchitraSwain : Re-ran the CI/Cd pipeline. Some test cases are failing. Please check and arrive at a good conclusion on the issues. |
|
@SuchitraSwain |
|
@seetadev please re-ran the pipeline |
|
Thank you @seetadev |
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.
PR #923 Review: Input Validation and Error Handling Improvements
Overview
This PR adds comprehensive input validation, exception handling, and test coverage across multiple libp2p modules. While it references Issue #194 (which is already resolved), the actual changes provide significant value for code quality and defensive programming.
Key Changes
✅ Real Value Added
- Input Validation: Added checks for empty protocol IDs, null handlers, negative deadlines
- Exception Handling: Proper error messages and exception chaining
- Test Coverage: 500+ lines of new tests covering edge cases
- API Consistency: Unified error handling patterns across modules
Files Modified (25 files)
- Core:
abc.py,basic_host.py,autonat.py - Stream Muxers:
mplex_stream.py,yamux.py - Transport:
quic/stream.py,tcp/tcp.py - Tests: Comprehensive test coverage added
Technical Improvements
Before vs After
# Before: No validation, always returned True
def set_deadline(self, ttl: int) -> bool:
self.read_deadline = ttl
return True # Even for negative values!
# After: Proper validation and exceptions
def set_deadline(self, ttl: int) -> None:
try:
if ttl < 0:
raise ValueError("Deadline cannot be negative")
self.read_deadline = ttl
except Exception as e:
raise MuxedStreamError(f"Failed to set deadline: {e}") from eTest Coverage Added
New Test Files
test_identify_push_exception_handling.py(146 lines) - Exception handling tests- Enhanced
test_basic_host.py(151 lines) - Input validation tests - Enhanced
test_mplex_stream.py(96 lines) - Deadline validation tests
Test Quality
- Input Validation: Tests empty strings, null values, negative numbers
- Exception Handling: Tests proper error propagation and chaining
- Edge Cases: Comprehensive coverage of failure scenarios
Assessment
✅ Strengths
- Defensive Programming: Prevents runtime errors with invalid inputs
- Better UX: More descriptive error messages for debugging
- Comprehensive Testing: Extensive test coverage for edge cases
- Code Quality: Improves maintainability and reliability
⚠️ Issues
- Misleading Description: Claims to fix Issue #194 (already resolved)
- Wrong Premise: The True/False pattern was already eliminated years ago
Recommendation
REFACTOR, DON'T DISCARD
Update PR Description: Remove Issue #194 references, focus on actual improvements:
- Input validation and defensive programming
- Better error handling and debugging
- Comprehensive test coverage
- Code quality improvements
Keep the Code: The changes provide real value for:
- Preventing runtime errors
- Improving debugging experience
- Adding defensive programming
- Enhancing code reliability
Final Assessment
Status: ✅ Approve with description update
The PR has significant value but needs reframing. The code improvements are excellent - just update the description to focus on the actual benefits rather than the already-resolved issue.
Review based on PR #923 - SuchitraSwain:194-try-catch branch
|
@acul71 : Thank you so much for your review and feedback. Appreciate your great support. @SuchitraSwain , @bomanaps and @yashksaini-coder : Wish if you could address the valuable feedback points shared by @acul71 and make the requisite changes. Once done, kindly ping me. I'll do a final review and then merge. |
|
I will address the comment by today |
|
@yashksaini-coder , @SuchitraSwain : Re-ran the CI/CD pipeline. We will see the test results soon. |
|
@SuchitraSwain , @yashksaini-coder : This looks good for final review and merge. Please change the PR description as shared by @acul71 |
|
@seetadev it's done, please merge the PR. Thank you for the great support |
|
@SuchitraSwain : Wish to have @acul71 and @pacrob 's review on this PR. Also, did you mistakenly close the PR request? |
yes it was by mistake. |
- Fix TCPListener.listen return type to match IListener interface - Fix BasicHost.set_stream_handler boolean context issue - Update demo code to handle new push_identify_to_peer signature - Fix test type errors with TProtocol casting and mock objects - Resolve all linting and formatting issues - All tests now pass with make pr command
|
Hi @SuchitraSwain . I've been fixing some lint and typecheck errors and fix identify (thanks @pacrob ) |
|
@yashksaini-coder and @SuchitraSwain : Thank you for updating the PR. Wish to check if we have covered all the feedback points as shared by @pacrob and me. We will do a final review once we receive a thumbs up from both of you. CCing @acul71 for his feedback and review too. |
|
@seetadev yes all the comments are addressed |
@SuchitraSwain : Great to hear. I'll wait to hear for a review from @acul71. Will do a final review + merge after discussion in the maintainer's call. |
Cute Animal Picture