[NX-101 Issue] Refactor error handling to use consistent API error format
Replaced all inline error messages with the standardized `api_error` helper for consistent error response formatting. This improves clarity, maintainability, and ensures uniform error structures across the application. Updated logging for collector failures to include error class and switched to warning level for target unreachable scenarios.
This commit is contained in:
@@ -9,6 +9,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.db import get_db
|
||||
from app.core.deps import require_roles
|
||||
from app.core.errors import api_error
|
||||
from app.models.models import EmailNotificationSettings, User
|
||||
from app.schemas.admin_settings import EmailSettingsOut, EmailSettingsTestRequest, EmailSettingsUpdate
|
||||
from app.services.audit import write_audit_log
|
||||
@@ -96,9 +97,9 @@ async def test_email_settings(
|
||||
) -> dict:
|
||||
settings = await _get_or_create_settings(db)
|
||||
if not settings.smtp_host:
|
||||
raise HTTPException(status_code=400, detail="SMTP host is not configured")
|
||||
raise HTTPException(status_code=400, detail=api_error("smtp_host_missing", "SMTP host is not configured"))
|
||||
if not settings.from_email:
|
||||
raise HTTPException(status_code=400, detail="From email is not configured")
|
||||
raise HTTPException(status_code=400, detail=api_error("smtp_from_email_missing", "From email is not configured"))
|
||||
|
||||
password = decrypt_secret(settings.encrypted_smtp_password) if settings.encrypted_smtp_password else None
|
||||
message = EmailMessage()
|
||||
@@ -126,7 +127,10 @@ async def test_email_settings(
|
||||
smtp.login(settings.smtp_username, password or "")
|
||||
smtp.send_message(message)
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=400, detail=f"SMTP test failed: {exc}")
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=api_error("smtp_test_failed", "SMTP test failed", {"error": str(exc)}),
|
||||
) from exc
|
||||
|
||||
await write_audit_log(db, "admin.email_settings.test", admin.id, {"recipient": str(payload.recipient)})
|
||||
return {"status": "sent", "recipient": str(payload.recipient)}
|
||||
|
||||
Reference in New Issue
Block a user