refactor: Remove duplicate router-level exception helpers
All routers now let domain exceptions propagate to the global handlers in main.py instead of catching and converting them to HTTPException. This eliminates: - Duplicate exception-to-HTTP-status mappings across 8 routers - Duplicate helper functions (_bad_gateway, _not_found, _conflict, etc.) - Inconsistent error response formats Changes: - Removed all try/except blocks from routers that catch domain exceptions - Removed duplicate helper functions from all routers - Added missing exception handlers to main.py for: * ActionNameError * FilterNameError * JailNameError * JailNotFoundInConfigError * FilterInvalidRegexError - Removed unused imports from affected routers All domain exceptions now propagate to the single authoritative mapping in main.py, ensuring consistent error codes, messages, and logging across the API. Affected routers: - action_config.py: Removed _action_not_found, _bad_request, _not_found helpers - bans.py: Removed try/except in ban/unban endpoints - config_misc.py: Removed try/except blocks - file_config.py: Removed 6 try/except blocks and _service_unavailable helper - filter_config.py: Removed try/except blocks - geo.py: Removed try/except in lookup_ip endpoint - jail_config.py: Removed try/except blocks - jails.py: Removed try/except blocks - server.py: Removed try/except blocks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -251,6 +251,33 @@ async def jail_not_found_handler(request: Request, exc: JailNotFoundError) -> JS
|
||||
return JSONResponse(status_code=404, content={"detail": f"Jail '{exc.name}' not found"})
|
||||
```
|
||||
|
||||
### Routers and Exception Propagation
|
||||
|
||||
- **Routers must NOT construct `HTTPException` for domain errors** — let domain exceptions propagate.
|
||||
- Routers should never have helper functions like `_bad_gateway()`, `_not_found()`, `_conflict()` etc. that convert domain exceptions to `HTTPException`.
|
||||
- All domain exception types must have corresponding handlers registered in `main.py` via `app.add_exception_handler()`.
|
||||
- Exception handlers are registered in order from most specific to least specific — FastAPI evaluates them in registration order.
|
||||
|
||||
```python
|
||||
# ❌ BAD — routers constructing HTTPException for domain exceptions
|
||||
@router.get("/{name}")
|
||||
async def get_jail(name: str, socket_path: Fail2BanSocketDep) -> JailDetailResponse:
|
||||
try:
|
||||
return await jail_service.get_jail(socket_path, name)
|
||||
except JailNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=f"Jail not found: {name!r}") from None
|
||||
|
||||
# ✅ GOOD — domain exception propagates to global handler
|
||||
@router.get("/{name}")
|
||||
async def get_jail(name: str, socket_path: Fail2BanSocketDep) -> JailDetailResponse:
|
||||
return await jail_service.get_jail(socket_path, name)
|
||||
```
|
||||
|
||||
All domain exceptions raised by services propagate to handlers in `main.py`, ensuring:
|
||||
1. Consistent error response format across the entire API.
|
||||
2. No duplicated exception-to-HTTP-status mapping logic.
|
||||
3. Easy to audit all error codes — they are all in one place.
|
||||
|
||||
---
|
||||
|
||||
## 9. Testing
|
||||
|
||||
Reference in New Issue
Block a user