From fc9fa790504330cfddcd24aac85cddd0b4025d50 Mon Sep 17 00:00:00 2001 From: Raykov-MS Date: Wed, 29 Jul 2026 11:14:04 +0300 Subject: [PATCH] try to know error in smb --- app/samba_access.py | 53 ++++++++++++++++++++++++++------- tests/unit/test_samba_access.py | 15 ++++++++++ 2 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 tests/unit/test_samba_access.py diff --git a/app/samba_access.py b/app/samba_access.py index 811f921..00445da 100644 --- a/app/samba_access.py +++ b/app/samba_access.py @@ -29,17 +29,21 @@ def check_access_and_list_files( try: samba_conn = connect_samba(smb_base_path=smb_base_path, user_params=user_params) except RuntimeError as exc: - return False, str(exc), [] + return False, _format_connect_error(str(exc)), [] try: ensure_dir_exists(input_dir, samba_conn=samba_conn) except Exception as exc: # noqa: BLE001 - return False, f"Не удалось создать входную папку: {input_dir}. {exc}", [] + return ( + False, + (f"Ошибка этапа создания входной папки Input: {input_dir}. {exc}"), + [], + ) if not samba_conn.path_exists(input_dir): return ( False, ( - "Не удалось создать входную папку: " + "Ошибка этапа создания входной папки Input: " f"{input_dir}. Проверьте права на родительский каталог." ), [], @@ -47,14 +51,41 @@ def check_access_and_list_files( ok, error = check_input_dir_access(Path(input_dir), samba_conn=samba_conn) if not ok: - return False, error or "Ошибка доступа к входной папке.", [] + return ( + False, + ( + "Ошибка этапа проверки доступа к папке Input: " + f"{error or 'нет прав на чтение/листинг.'}" + ), + [], + ) - with samba_conn: - entries = sorted(samba_conn.listdir(input_dir)) - files: list[str] = [] - for name in entries: - full_path = input_dir.rstrip("/\\") + "\\" + name - if samba_conn.is_file(full_path): - files.append(name) + try: + with samba_conn: + entries = sorted(samba_conn.listdir(input_dir)) + files: list[str] = [] + for name in entries: + full_path = input_dir.rstrip("/\\") + "\\" + name + if samba_conn.is_file(full_path): + files.append(name) + except Exception as exc: # noqa: BLE001 + return ( + False, + (f"Ошибка этапа чтения содержимого папки Input: {input_dir}. {exc}"), + [], + ) return True, "", files + + +def _format_connect_error(details: str) -> str: + normalized = details.lower() + auth_hints = ( + "status_logon_failure", + "logon failure", + "authentication", + "no username or password", + ) + if any(hint in normalized for hint in auth_hints): + return f"Ошибка этапа подключения к SMB (аутентификация). {details}" + return f"Ошибка этапа подключения к SMB. {details}" diff --git a/tests/unit/test_samba_access.py b/tests/unit/test_samba_access.py new file mode 100644 index 0000000..902a7f5 --- /dev/null +++ b/tests/unit/test_samba_access.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +from app.samba_access import _format_connect_error + + +def test_format_connect_error_marks_authentication_stage() -> None: + message = _format_connect_error("Ошибка подключения к SMB: STATUS_LOGON_FAILURE") + assert "аутентификация" in message.lower() + assert "STATUS_LOGON_FAILURE" in message + + +def test_format_connect_error_marks_generic_connect_stage() -> None: + message = _format_connect_error("Ошибка подключения к SMB: timed out") + assert message.startswith("Ошибка этапа подключения к SMB.") + assert "аутентификация" not in message.lower()