Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(ai): raise a typed error for a malformed attachment manifest

Juan Denis009d9c4d7f49
diff --git a/backend/app/api/ai.py b/backend/app/api/ai.py
index 082d9f94..a99375bc 100644
--- a/backend/app/api/ai.py
+++ b/backend/app/api/ai.py
@@ -35,7 +35,6 @@ from app.middleware.rbac import admin_required, get_current_user
from app.models.ai import AiConversation, AiMessage, AiPendingAction
from app.services import ai_service
from app.services.ai_attachment_registry import (
- AttachmentValidationError,
normalize_references,
resolve_attachments,
)
@@ -293,10 +292,9 @@ def chat():
return jsonify({'error': 'message is required'}), 400
mode = data.get('mode') or AiConversation.MODE_ASSISTANT
page_context = data.get('page_context') or {}
- try:
- attachment_refs = _attachment_references(data)
- except AttachmentValidationError as exc:
- return jsonify({'error': str(exc)}), 400
+ # AttachmentValidationError is typed (app.exceptions.ValidationError), so the
+ # global handler renders the 400 with a code and request_id.
+ attachment_refs = _attachment_references(data)
row = _load_or_create(data.get('conversation_id'), user, mode)
if row is None:
@@ -344,10 +342,9 @@ def chat_stream():
return jsonify({'error': 'message is required'}), 400
mode = data.get('mode') or AiConversation.MODE_ASSISTANT
page_context = data.get('page_context') or {}
- try:
- attachment_refs = _attachment_references(data)
- except AttachmentValidationError as exc:
- return jsonify({'error': str(exc)}), 400
+ # AttachmentValidationError is typed (app.exceptions.ValidationError), so the
+ # global handler renders the 400 with a code and request_id.
+ attachment_refs = _attachment_references(data)
row = _load_or_create(data.get('conversation_id'), user, mode)
if row is None:
diff --git a/backend/app/services/ai_attachment_registry.py b/backend/app/services/ai_attachment_registry.py
index 89894e9a..1e3d7fe7 100644
--- a/backend/app/services/ai_attachment_registry.py
+++ b/backend/app/services/ai_attachment_registry.py
@@ -15,6 +15,7 @@ from datetime import datetime, timezone
from threading import RLock
from typing import Any, Callable
+from app.exceptions import ValidationError
from app.utils.sensitive_data_filter import mask_payload
logger = logging.getLogger(__name__)
@@ -26,8 +27,16 @@ MAX_LABEL_CHARS = 160
_TYPE_RE = re.compile(r'^[a-z0-9][a-z0-9._-]{0,63}$')
-class AttachmentValidationError(ValueError):
- """The submitted attachment manifest is malformed or exceeds its cap."""
+class AttachmentValidationError(ValidationError):
+ """The submitted attachment manifest is malformed or exceeds its cap.
+
+ A typed error, so the global handler owns the 400 body, the ``code``, and
+ the ``X-Request-ID`` correlation -- routes do not hand-shape it. Still a
+ ``ValueError`` (via ``app.exceptions.ValidationError``) for existing
+ ``except ValueError`` callers.
+ """
+
+ code = 'attachment_invalid'
class AttachmentDeniedError(PermissionError):