Skip to content

Commit

Permalink
Wrap UnexpectedCharacters error in as ObservationConfigError
Browse files Browse the repository at this point in the history
  • Loading branch information
Yngve S. Kristiansen committed Jul 19, 2023
1 parent 7f73b66 commit 08521dd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/ert/parsing/new_observations_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from enum import Enum, auto
from typing import Any, Dict, List, Literal, Tuple, TypedDict, Union

from lark import Lark, Transformer
from lark import Lark, Transformer, UnexpectedCharacters
from typing_extensions import NotRequired

from .config_errors import ConfigValidationError
Expand Down Expand Up @@ -105,9 +105,28 @@ def _parse_content(
Tuple[ObservationType, FileContextToken, Dict[FileContextToken, Any]],
]
]:
return (FileContextTransformer(filename) * TreeToObservations()).transform(
observations_parser.parse(content)
)
try:
return (FileContextTransformer(filename) * TreeToObservations()).transform(
observations_parser.parse(content)
)
except UnexpectedCharacters as e:
unexpected_char = e.char
allowed_chars = e.allowed
message = (
f"Observation parsing failed: Did not expect character: {unexpected_char}. "
f"Expected one of {allowed_chars}"
)

raise ObservationConfigError.from_info(
ErrorInfo(
filename=filename,
message=message,
line=e.line,
end_line=e.line + 1,
column=e.column,
end_column=e.column + 1,
)
)


observations_parser = Lark(
Expand Down
49 changes: 48 additions & 1 deletion tests/test_config_parsing/test_new_observations_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from ert.parsing.new_observations_parser import ObservationType, _parse_content
import pytest

from ert.parsing.new_observations_parser import (
ObservationConfigError,
ObservationType,
_parse_content,
)


def test_parse():
Expand Down Expand Up @@ -68,3 +74,44 @@ def test_parse():
),
]
)


def test_parse_include_outside_raises_obsconf_error():
with pytest.raises(
ObservationConfigError,
match="Observation parsing failed: Did not expect character",
):
_parse_content(
"""
include a;
HISTORY_OBSERVATION FOPR;
SUMMARY_OBSERVATION WOPR_OP1_9
{
VALUE = 0.1;
ERROR = 0.05;
DATE = 2010-03-31; -- (RESTART = 9)
KEY = WOPR:OP1;
};
GENERAL_OBSERVATION WPR_DIFF_1 {
DATA = SNAKE_OIL_WPR_DIFF;
INDEX_LIST = 400,800,1200,1800;
DATE = 2015-06-13; -- (RESTART = 199)
OBS_FILE = wpr_diff_obs.txt;
};
HISTORY_OBSERVATION FOPR
{
ERROR = 0.1;
SEGMENT SEG
{
START = 1;
STOP = 0;
ERROR = -1;
};
};
""",
"",
)

0 comments on commit 08521dd

Please sign in to comment.