Skip to content

Commit bd2bf30

Browse files
committed
fix: errors
1 parent 3f0ce64 commit bd2bf30

File tree

2 files changed

+83
-12
lines changed

2 files changed

+83
-12
lines changed

application/api/user/attachments/routes.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ def _enforce_uploaded_audio_size_limit(file, filename: str) -> None:
8686
enforce_audio_file_size_limit(size_bytes)
8787

8888

89+
def _get_store_attachment_user_error(exc: Exception) -> str:
90+
if isinstance(exc, AudioFileTooLargeError):
91+
return build_stt_file_size_limit_message()
92+
return "Failed to process file"
93+
94+
8995
def _require_live_stt_redis():
9096
redis_client = get_redis_instance()
9197
if redis_client:
@@ -176,7 +182,7 @@ def post(self):
176182
errors.append({
177183
"upload_index": idx,
178184
"filename": file.filename,
179-
"error": str(file_err)
185+
"error": _get_store_attachment_user_error(file_err),
180186
})
181187

182188
if not tasks:
@@ -473,12 +479,27 @@ def post(self):
473479
if not session_state.get("language") and transcript.get("language"):
474480
session_state["language"] = transcript["language"]
475481

476-
apply_live_stt_hypothesis(
477-
session_state,
478-
str(transcript.get("text", "")),
479-
chunk_index,
480-
is_silence=is_silence,
481-
)
482+
try:
483+
apply_live_stt_hypothesis(
484+
session_state,
485+
str(transcript.get("text", "")),
486+
chunk_index,
487+
is_silence=is_silence,
488+
)
489+
except ValueError:
490+
current_app.logger.warning(
491+
"Invalid live transcription chunk",
492+
exc_info=True,
493+
)
494+
return make_response(
495+
jsonify(
496+
{
497+
"success": False,
498+
"message": "Invalid live transcription chunk",
499+
}
500+
),
501+
409,
502+
)
482503
save_live_stt_session(redis_client, session_state)
483504

484505
return make_response(
@@ -505,11 +526,6 @@ def post(self):
505526
),
506527
200,
507528
)
508-
except ValueError as err:
509-
return make_response(
510-
jsonify({"success": False, "message": str(err)}),
511-
409,
512-
)
513529
except Exception as err:
514530
current_app.logger.error(
515531
f"Error transcribing live audio chunk: {err}", exc_info=True

tests/api/user/attachments/test_routes.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def save_file(file, path):
7676
assert _get_response_status(response) == 200
7777
assert [task["upload_index"] for task in payload["tasks"]] == [0, 2]
7878
assert payload["errors"][0]["upload_index"] == 1
79+
assert payload["errors"][0]["error"] == "Failed to process file"
7980

8081
@patch("application.api.user.tasks.store_attachment.delay")
8182
@patch("application.stt.upload_limits.settings")
@@ -380,3 +381,57 @@ def test_live_stt_chunk_rejects_missing_session(
380381

381382
assert _get_response_status(response) == 404
382383
assert _get_response_json(response)["message"] == "Live transcription session not found"
384+
385+
@patch("application.api.user.attachments.routes.STTCreator.create_stt")
386+
@patch("application.api.user.attachments.routes.get_redis_instance")
387+
def test_live_stt_chunk_hides_internal_value_errors(
388+
self, mock_get_redis, mock_create_stt, flask_app, mock_mongo_db
389+
):
390+
from application.api.user.attachments.routes import (
391+
LiveSpeechToTextChunk,
392+
LiveSpeechToTextStart,
393+
)
394+
395+
app = Flask(__name__)
396+
fake_redis = FakeRedis()
397+
mock_get_redis.return_value = fake_redis
398+
399+
start_resource = LiveSpeechToTextStart()
400+
with app.test_request_context(
401+
"/api/stt/live/start",
402+
method="POST",
403+
json={"language": "ru"},
404+
):
405+
request.decoded_token = {"sub": "test_user"}
406+
start_response = start_resource.post()
407+
session_id = _get_response_json(start_response)["session_id"]
408+
409+
mock_stt = MagicMock()
410+
mock_stt.transcribe.return_value = {
411+
"text": "hello there",
412+
"language": "ru",
413+
"duration_s": 1.0,
414+
"segments": [],
415+
"provider": "openai",
416+
}
417+
mock_create_stt.return_value = mock_stt
418+
419+
chunk_resource = LiveSpeechToTextChunk()
420+
with app.test_request_context(
421+
"/api/stt/live/chunk",
422+
method="POST",
423+
data={
424+
"session_id": session_id,
425+
"chunk_index": "-1",
426+
"file": (io.BytesIO(b"chunk-neg"), "chunk-neg.wav"),
427+
},
428+
content_type="multipart/form-data",
429+
):
430+
request.decoded_token = {"sub": "test_user"}
431+
response = chunk_resource.post()
432+
433+
assert _get_response_status(response) == 409
434+
assert (
435+
_get_response_json(response)["message"]
436+
== "Invalid live transcription chunk"
437+
)

0 commit comments

Comments
 (0)