Replace bare except with specific exception handling in backend/apps/slack/models/event.py
- Dominant language
- Python
- Stars
- 451
- Forks
- 707
- Avg merge
- 22h 59m
- Merged PRs (30d)
- 91
Description
## **Describe the bug**
In `backend/apps/slack/models/event.py` at line 51-52, there is a bare `except` clause with a `pass` statement that silently swallows exceptions without any logging or handling:
```python
try:
command, *args = text.strip().split()
text = " ".join(args)
except ValueError:
pass
```
While this code catches `ValueError` specifically, the bare `pass` statement masks the exception entirely. This violates best practices:
1. **Silent failures**: Errors are silently ignored with no logging, making debugging difficult
2. **Maintainability**: Future maintainers won't understand why the exception is caught but not handled
3. **SonarQube compliance**: This pattern is flagged as a code quality issue
## **To Reproduce**
1. Go to `backend/apps/slack/models/event.py`
2. Look at lines 51-52 in the `__init__` method
3. Observe that `ValueError` is caught but silently ignored with `pass`
## **Expected behavior**
When a `ValueError` occurs during text parsing (e.g., when `text.strip().split()` produces an empty result), the exception should be:
- **Logged** at an appropriate level (DEBUG or WARNING) so developers can debug issues
- **Handled gracefully** with sensible default values or error context
## **Proposed solution**
Replace the bare `except ValueError: pass` with:
```python
except ValueError:
logger.debug("Failed to parse command text: %s. Using defaults.", text, exc_info=True)
# Keep defaults: command and text remain unchanged
```
This:
- Logs the exception for debugging purposes
- Documents the expected behavior
- Complies with code quality standards
- Preserves backward compatibility (defaults are used when parsing fails)
## **Additional context**
This pattern is identified as a best practice improvement in Python and aligns with:
- [PEP 8 Error Handling](https://pep8.org/#programming-recommendations)
- SonarQube code quality standards
- OWASP secure coding practices
Similar improvements could be made across the codebase for consistency.
## **Are you going to work on implementing this?**
- [x] Yes
- [ ] No
Contributor guide
Assessment
This issue has not been assessed yet.