From 1b75f6004a155983186f5df732e35ebd49ef6224 Mon Sep 17 00:00:00 2001 From: Jonathan Karlsen Date: Tue, 3 Dec 2024 12:24:46 +0100 Subject: [PATCH] Improve UX for permission errors in storage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit: * Improves the error message displayed when the dark storage server does not have access to the storage path. * Makes the dark storage server return a response with status code 401 - unauthorized when the `get_ensemble_record` endpoint fails due to `PermissionError`. * Makes the failed message in `LegacyEnsemble._evaluate_inner` omit stacktrace when it failed due to PermissionError, making it shorter and more consise. --- src/ert/__main__.py | 3 ++- .../ert/unit_tests/shared/test_main_entry.py | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ert/__main__.py b/src/ert/__main__.py index 8eacf28147b..45c892c1cbe 100755 --- a/src/ert/__main__.py +++ b/src/ert/__main__.py @@ -36,6 +36,7 @@ from ert.run_models.multiple_data_assimilation import MultipleDataAssimilation from ert.services import StorageService, WebvizErt from ert.shared.storage.command import add_parser_options as ert_api_add_parser_options +from ert.storage import ErtStorageException from ert.trace import trace, tracer, tracer_provider from ert.validation import ( IntegerArgument, @@ -673,7 +674,7 @@ def main() -> None: ) as context: logger.info(f"Running ert with {args}") args.func(args, context.plugin_manager) - except ErtCliError as err: + except (ErtCliError, ErtStorageException) as err: span.set_status(Status(StatusCode.ERROR)) span.record_exception(err) logger.debug(str(err)) diff --git a/tests/ert/unit_tests/shared/test_main_entry.py b/tests/ert/unit_tests/shared/test_main_entry.py index 52667145645..c58941dab23 100644 --- a/tests/ert/unit_tests/shared/test_main_entry.py +++ b/tests/ert/unit_tests/shared/test_main_entry.py @@ -2,8 +2,10 @@ import sys from unittest.mock import MagicMock +import filelock import pytest +import ert import ert.__main__ as main @@ -32,3 +34,21 @@ def test_main_logging_argparse(monkeypatch, caplog): with caplog.at_level(logging.INFO): main.main() assert "mode='test_run'" in caplog.text + + +@pytest.mark.usefixtures("copy_poly_case") +def test_storage_exception_is_not_unexpected_error(monkeypatch, caplog): + file_lock_mock = MagicMock() + caplog.set_level(logging.ERROR) + + def mock_acquire(*args, **kwargs): + raise filelock.Timeout + + file_lock_mock.acquire = mock_acquire + monkeypatch.setattr(ert.storage.local_storage, "FileLock", file_lock_mock) + + monkeypatch.setattr(sys, "argv", ["ert", "test_run", "poly.ert"]) + with pytest.raises(SystemExit) as exc_info: + main.main() + assert "ERT crashed unexpectedly" not in str(exc_info.value) + assert "Failed to open storage" in str(exc_info.value)