fix: handle exceptions without 'detail' attribute in error handler#259
Open
rasadov wants to merge 2 commits intolaurentS:masterfrom
Open
fix: handle exceptions without 'detail' attribute in error handler#259rasadov wants to merge 2 commits intolaurentS:masterfrom
rasadov wants to merge 2 commits intolaurentS:masterfrom
Conversation
- Change _rate_limit_exceeded_handler to accept Exception instead of RateLimitExceeded - Use getattr() to safely access exc.detail with fallback - Prevents AttributeError when ConnectionError or other exceptions occur - Add tests for ConnectionError and RateLimitExceeded scenarios Fixes laurentS#253
Adds comprehensive test coverage for the exception handler fix, ensuring both RateLimitExceeded and exceptions without detail attributes are handled correctly.
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
Fixes #253
Reopening of #254 - When Redis (or another storage backend) is unavailable, slowapi's error handler tries to access
exc.detailon aConnectionErrorexception, which doesn't have this attribute. This causes a secondaryAttributeErrorthat masks the original connection failure and crashes the application.Changes
_rate_limit_exceeded_handlerto acceptExceptioninstead ofRateLimitExceededgetattr(exc, 'detail', "Rate limit exceeded")to safely access thedetailattributeRateLimitExceeded, orstr(exc)for other exceptionsRateLimitExceededand exceptions withoutdetailscenariosRoot Cause
The error handler in
slowapi/extension.pydirectly accessedexc.detailwithout checking if the attribute exists. When aConnectionError(or other exception types) is raised during rate limit checking, this assumption fails becauseConnectionErrordoesn't have adetailattribute.Testing
test_rate_limit_exceeded_handler_with_detailto verify normalRateLimitExceededexceptions still work correctlytest_rate_limit_exceeded_handler_without_detailto verify exceptions without adetailattribute are handled gracefullyBackward Compatibility
✅ No breaking changes - existing code using
RateLimitExceededcontinues to work as before✅ Improved error handling for edge cases (connection failures)
Closes #253