Skip to content

Commit

Permalink
Propagate errors in opening storage to Suggestor
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindeide committed Sep 6, 2024
1 parent e29219b commit c0d8986
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
19 changes: 15 additions & 4 deletions src/ert/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from qtpy.QtGui import QIcon
from qtpy.QtWidgets import QApplication, QWidget

from ert.config import ConfigValidationError, ConfigWarning, ErtConfig
from ert.config import ConfigValidationError, ConfigWarning, ErrorInfo, ErtConfig
from ert.gui.main_window import ErtMainWindow
from ert.gui.simulation import ExperimentPanel
from ert.gui.tools.event_viewer import (
Expand All @@ -37,7 +37,7 @@
from ert.namespace import Namespace
from ert.plugins import ErtPluginManager
from ert.services import StorageService
from ert.storage import Storage, open_storage
from ert.storage import ErtStorageException, Storage, open_storage
from ert.storage.local_storage import local_storage_set_ert_config

from .suggestor import Suggestor
Expand Down Expand Up @@ -89,6 +89,7 @@ def _start_initial_gui_window(
logger = logging.getLogger(__name__)
error_messages = []
config_warnings = []
deprecations = []
ert_config = None

with warnings.catch_warnings(record=True) as all_warnings:
Expand Down Expand Up @@ -116,7 +117,17 @@ def _start_initial_gui_window(
and cast(ConfigWarning, w.message).info.is_deprecation
]
error_messages += error.errors
logger.info("Error in config file shown in gui: '%s'", str(error))
if ert_config is not None:
try:
storage = open_storage(ert_config.ens_path, mode="w")
except ErtStorageException as err:
error_messages.append(
ErrorInfo(f"Error opening storage in ENSPATH: {err}").set_context(
ert_config.ens_path
)
)
if error_messages:
logger.info(f"Error in config file shown in gui: {error_messages}")
return (
Suggestor(
error_messages,
Expand All @@ -131,6 +142,7 @@ def _start_initial_gui_window(
),
None,
)
assert ert_config is not None
config_warnings = [
cast(ConfigWarning, w.message).info
for w in all_warnings
Expand Down Expand Up @@ -159,7 +171,6 @@ def _start_initial_gui_window(
logger.info("Suggestion shown in gui '%s'", msg)
for msg in config_warnings:
logger.info("Warning shown in gui '%s'", msg)
storage = open_storage(ert_config.ens_path, mode="w")
_main_window = _setup_main_window(
ert_config, args, log_handler, storage, plugin_manager
)
Expand Down
11 changes: 10 additions & 1 deletion src/ert/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@
EnsembleAccessor = LocalEnsemble


class ErtStorageException(Exception):
pass


def open_storage(
path: Union[str, os.PathLike[str]], mode: Union[ModeLiteral, Mode] = "r"
) -> Storage:
return LocalStorage(Path(path), Mode(mode))
try:
return LocalStorage(Path(path), Mode(mode))
except Exception as err:
raise ErtStorageException(
f"Failed to open storage: {path} with error: {err}"
) from err


__all__ = [
Expand Down
19 changes: 14 additions & 5 deletions tests/unit_tests/storage/test_local_storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import shutil
import tempfile
from dataclasses import dataclass, field
Expand Down Expand Up @@ -32,7 +33,7 @@
from ert.config.gen_kw_config import TransformFunctionDefinition
from ert.config.general_observation import GenObservation
from ert.config.observation_vector import ObsVector
from ert.storage import open_storage
from ert.storage import ErtStorageException, open_storage
from ert.storage.local_storage import _LOCAL_STORAGE_VERSION
from ert.storage.mode import ModeError
from ert.storage.realization_storage_state import RealizationStorageState
Expand Down Expand Up @@ -262,6 +263,14 @@ def test_open_storage_nested_dirs(tmp_path, caplog):
assert storage.path.exists()


def test_open_storage_with_corrupted_storage(tmp_path):
with open_storage(tmp_path / "storage", mode="w") as storage:
storage.create_experiment().create_ensemble(name="prior", ensemble_size=1)
os.remove(tmp_path / "storage" / "index.json")
with pytest.raises(ErtStorageException, match="No index.json"):
open_storage(tmp_path / "storage", mode="w")


def test_that_open_storage_in_read_mode_with_newer_version_throws_exception(
tmp_path, caplog
):
Expand All @@ -270,7 +279,7 @@ def test_that_open_storage_in_read_mode_with_newer_version_throws_exception(
storage._save_index()

with pytest.raises(
RuntimeError,
ErtStorageException,
match=f"Cannot open storage '{tmp_path}': Storage version {_LOCAL_STORAGE_VERSION+1} is newer than the current version {_LOCAL_STORAGE_VERSION}, upgrade ert to continue, or run with a different ENSPATH",
):
open_storage(tmp_path, mode="r")
Expand All @@ -284,7 +293,7 @@ def test_that_open_storage_in_read_mode_with_older_version_throws_exception(
storage._save_index()

with pytest.raises(
RuntimeError,
ErtStorageException,
match=f"Cannot open storage '{tmp_path}' in read-only mode: Storage version {_LOCAL_STORAGE_VERSION-1} is too old",
):
open_storage(tmp_path, mode="r")
Expand All @@ -298,7 +307,7 @@ def test_that_open_storage_in_write_mode_with_newer_version_throws_exception(
storage._save_index()

with pytest.raises(
RuntimeError,
ErtStorageException,
match=f"Cannot open storage '{tmp_path}': Storage version {_LOCAL_STORAGE_VERSION+1} is newer than the current version {_LOCAL_STORAGE_VERSION}, upgrade ert to continue, or run with a different ENSPATH",
):
open_storage(tmp_path, mode="w")
Expand Down Expand Up @@ -534,7 +543,7 @@ def double_open_timeout(self):
# already opened with mode="w" somewhere else
with patch(
"ert.storage.local_storage.LocalStorage.LOCK_TIMEOUT", 0.0
), pytest.raises(TimeoutError):
), pytest.raises(ErtStorageException):
open_storage(self.tmpdir + "/storage/", mode="w")

@rule()
Expand Down

0 comments on commit c0d8986

Please sign in to comment.