Skip to content

fix: handle exceptions without 'detail' attribute in error handler#259

Open
rasadov wants to merge 2 commits intolaurentS:masterfrom
rasadov:fix/213-exception-detail
Open

fix: handle exceptions without 'detail' attribute in error handler#259
rasadov wants to merge 2 commits intolaurentS:masterfrom
rasadov:fix/213-exception-detail

Conversation

@rasadov
Copy link

@rasadov rasadov commented Feb 9, 2026

Summary

Fixes #253

Reopening of #254 - When Redis (or another storage backend) is unavailable, slowapi's error handler tries to access exc.detail on a ConnectionError exception, which doesn't have this attribute. This causes a secondary AttributeError that masks the original connection failure and crashes the application.

Changes

  • Modified _rate_limit_exceeded_handler to accept Exception instead of RateLimitExceeded
  • Used getattr(exc, 'detail', "Rate limit exceeded") to safely access the detail attribute
  • Added fallback logic: use default message for RateLimitExceeded, or str(exc) for other exceptions
  • Added comprehensive tests for both RateLimitExceeded and exceptions without detail scenarios

Root Cause

The error handler in slowapi/extension.py directly accessed exc.detail without checking if the attribute exists. When a ConnectionError (or other exception types) is raised during rate limit checking, this assumption fails because ConnectionError doesn't have a detail attribute.

Testing

  • Added test test_rate_limit_exceeded_handler_with_detail to verify normal RateLimitExceeded exceptions still work correctly
  • Added test test_rate_limit_exceeded_handler_without_detail to verify exceptions without a detail attribute are handled gracefully

Backward Compatibility

✅ No breaking changes - existing code using RateLimitExceeded continues to work as before
✅ Improved error handling for edge cases (connection failures)

Closes #253

- 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: slowapi error handler assumes exc.detail, causing AttributeError with ConnectionError

1 participant