Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/api/account/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,5 @@ async def change_user_password_api(

except AuthenticationException:
raise HTTPException(status_code=401, detail="Current password is incorrect")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
except Exception:
raise HTTPException(status_code=500)
4 changes: 2 additions & 2 deletions backend/api/account/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ async def change_password(
return True
except AuthenticationException:
raise
except Exception:
raise ServerException("Failed to change password")
except Exception as e:
raise ServerException(f"Failed to change password: {e}")
8 changes: 4 additions & 4 deletions backend/api/auth/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,16 @@ async def logout(
await db.commit()

return True
except Exception:
raise ServerException("Logout failed")
except Exception as e:
raise ServerException(f"Logout failed: {e}")


async def logout_all_devices(db: AsyncSession, redis_client: redis.Redis, user_id: str) -> bool:
"""Logout user from all devices"""
try:
return await clear_user_all_sessions(db, redis_client, user_id)
except Exception:
raise ServerException("Failed to logout all devices")
except Exception as e:
raise ServerException(f"Failed to logout all devices: {e}")


async def get_or_create_csrf_token(
Expand Down
3 changes: 1 addition & 2 deletions backend/api/roles/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
AuthorizationException,
ConflictException,
NotFoundException,
ServerException,
)
from utils.response import APIResponse, common_responses, parse_responses

Expand Down Expand Up @@ -121,7 +120,7 @@ async def update_role_api(
raise HTTPException(status_code=403, detail=e.message)
except ConflictException:
raise HTTPException(status_code=409, detail="Role name already exists")
except ServerException:
except Exception:
raise HTTPException(status_code=500)


Expand Down
4 changes: 2 additions & 2 deletions backend/core/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def get_db() -> AsyncSession:
await db.rollback()
raise
except Exception as e:
logger.error(f"Database error: {e}")
logger.error("Database error: %s", e, exc_info=True)
await db.rollback()
raise e

Expand All @@ -28,7 +28,7 @@ def get_sync_db():
try:
yield db
except Exception as e:
logger.error(f"Database error: {e}")
logger.error("Database error: %s", e, exc_info=True)
db.rollback()
raise e
finally:
Expand Down
11 changes: 11 additions & 0 deletions backend/extensions/exception_handler.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import logging

from fastapi import FastAPI, HTTPException, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

from utils.response import APIResponse

logger = logging.getLogger("http")


def add_exception_handlers(app: FastAPI):
@app.exception_handler(HTTPException)
Expand All @@ -30,5 +34,12 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE

@app.exception_handler(Exception)
async def internal_server_error_handler(request: Request, exc: Exception):
logger.error(
"Unhandled exception for %s %s: %s",
request.method,
request.url.path,
exc,
exc_info=(type(exc), exc, exc.__traceback__),
)
resp = APIResponse(code=500, message="Internal Server Error", data=None)
return JSONResponse(status_code=500, content=resp.dict(exclude_none=True))
10 changes: 9 additions & 1 deletion backend/extensions/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ def _open(self, timeout: int = 30) -> smtplib.SMTP:
if self._cfg.username and self._cfg.password:
client.login(self._cfg.username, self._cfg.password)
return client
except Exception:
except Exception as e:
logger.error(
"SMTP connection failed: host=%s port=%s encryption=%s error=%s",
self._cfg.host,
self._cfg.port,
enc,
e,
exc_info=True,
)
try:
client.quit()
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/api/account/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ async def test_change_password_service_error(
assert response.status_code == 500
data = response.json()
assert data["code"] == 500
assert data["message"] == "Database error"
assert data["message"] == "Internal Server Error"

@pytest.mark.asyncio
async def test_change_password_authentication_exception(
Expand Down
5 changes: 4 additions & 1 deletion backend/tests/extensions/test_smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,9 @@ def test_open_no_auth_validation_error(self):
mailer._open()
assert "SMTP_USERNAME" in str(exc_info.value) or "SMTP_PASSWORD" in str(exc_info.value)

@patch("extensions.smtp.logger")
@patch("extensions.smtp.smtplib.SMTP")
def test_open_connection_error(self, mock_smtp):
def test_open_connection_error(self, mock_smtp, mock_logger):
"""Test handling connection errors"""
cfg = SMTPSettings(
enabled=True,
Expand All @@ -355,6 +356,8 @@ def test_open_connection_error(self, mock_smtp):
mailer._open()
assert "Connection failed" in str(exc_info.value)
mock_client.quit.assert_called_once()
mock_logger.error.assert_called_once()
assert "Connection failed" in str(mock_logger.error.call_args)

@patch.object(SMTPMailer, "_open")
def test_send_text_plain(self, mock_open):
Expand Down
Loading