Fix #991: Add input validation and improve error handling in CartActionBean #1052
+155
−2
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.
Description:
This PR addresses the safety concerns raised in issue #991. I've added validation to prevent NPEs when workingItemId is null or blank, and made the exception handling more specific.
What changed:
I added null/blank checks at the beginning of addItemToCart() and removeItemFromCart() methods. When invalid input is detected, the methods now return early with an error message instead of crashing later. I also changed the catch block in updateCartQuantities() from catching generic Exception to specifically catching NumberFormatException, which is what we actually expect when parsing fails.
Tests:
Added a comprehensive test suite (CartActionBeanTest) that covers:
Null, empty, and blank workingItemId scenarios for both add and remove operations
Attempting to remove items that don't exist in the cart
Basic cart operations like clearing
All unit tests pass. The integration tests (ScreenTransitionIT) fail, but that's because they need a running server - not related to these changes.
Related:
Fixes #991