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:
@ -104,11 +104,20 @@ async def remove_user_api(username: str):
|
|||||||
A DetailResponse with a message indicating the user has been removed.
|
A DetailResponse with a message indicating the user has been removed.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
HTTPException: if an error occurs while removing the user.
|
HTTPException: 404 if the user is not found, 400 if another error occurs.
|
||||||
"""
|
"""
|
||||||
try:
|
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)
|
cli_api.remove_user(username)
|
||||||
return DetailResponse(detail=f'User {username} has been removed.')
|
return DetailResponse(detail=f'User {username} has been removed.')
|
||||||
|
except HTTPException:
|
||||||
|
|
||||||
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
|
raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user