Improve user removal API with proper 404 error handling

- Added user existence check before attempting removal
- Return 404 status code when user is not found
- Leveraged existing get_user functionality for consistency
- Separated HTTPException handling to preserve status codes
- Enhanced error handling to provide clearer feedback to API consumers
This commit is contained in:
Whispering Wind
2025-04-14 12:11:53 +03:30
committed by GitHub
parent 6d7dbc33f1
commit 3384095d9e

View File

@ -104,11 +104,20 @@ async def remove_user_api(username: str):
A DetailResponse with a message indicating the user has been removed.
Raises:
HTTPException: if an error occurs while removing the user.
HTTPException: 404 if the user is not found, 400 if another error occurs.
"""
try:
user = cli_api.get_user(username)
if not user:
raise HTTPException(status_code=404, detail=f'User {username} not found.')
cli_api.kick_user_by_name(username)
cli_api.traffic_status(display_output=False)
cli_api.remove_user(username)
return DetailResponse(detail=f'User {username} has been removed.')
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=400, detail=f'Error: {str(e)}')