Skip to content

Commit

Permalink
monkeypatching not going so well
Browse files Browse the repository at this point in the history
  • Loading branch information
TamarZanzouri committed Feb 14, 2024
1 parent 8316d7d commit 2bbb9ba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
11 changes: 11 additions & 0 deletions robot-server/robot_server/protocols/analysis_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
)


from opentrons_shared_data.errors import EnumeratedError, PythonException


class AnalysisStatus(str, Enum):
"""Status of a protocol analysis."""

Expand Down Expand Up @@ -138,4 +141,12 @@ class CompletedAnalysis(BaseModel):
)


async def map_unexpected_error(error: BaseException) -> EnumeratedError:
"""Map an unhandled Exception to a known exception."""
if isinstance(error, EnumeratedError):
return error
else:
return PythonException(error)


ProtocolAnalysis = Union[PendingAnalysis, CompletedAnalysis]
4 changes: 2 additions & 2 deletions robot-server/robot_server/protocols/protocol_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from opentrons import protocol_runner
from opentrons.protocol_engine.errors import ErrorOccurrence
import robot_server.protocols.analysis_models as eh

from .protocol_store import ProtocolResource
from .analysis_store import AnalysisStore
from ..errors.exception_handlers import map_unexpected_error

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -37,7 +37,7 @@ async def analyze(
protocol_source=protocol_resource.source, deck_configuration=[]
)
except Exception as error:
internal_error = await map_unexpected_error(error=error)
internal_error = await eh.map_unexpected_error(error=error)
print(f"runner raised exception with error: {internal_error}")
await self._analysis_store.update(
analysis_id=analysis_id,
Expand Down
27 changes: 26 additions & 1 deletion robot-server/tests/protocols/test_protocol_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
from robot_server.protocols.analysis_store import AnalysisStore
from robot_server.protocols.protocol_store import ProtocolResource
from robot_server.protocols.protocol_analyzer import ProtocolAnalyzer
import robot_server.protocols.analysis_models as eh


@pytest.fixture(autouse=True)
def patch_mock_map_unexpected_error(
decoy: Decoy, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Replace map_unexpected_error with a mock."""
mock = decoy.mock(func=eh.map_unexpected_error)
monkeypatch.setattr(eh.map_unexpected_error, "map_unexpected_error", mock)
decoy.when(mock(Exception("You got me!!"))).then_return(
pe_errors.ErrorOccurrence(
id="internal-error",
createdAt=datetime(year=2023, month=3, day=3),
errorType="PythonException",
detail="You got me!!",
)
)


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -171,6 +189,13 @@ async def test_analyze_updates_pending_on_error(
protocol_key="dummy-data-111",
)

internal_error = pe_errors.ErrorOccurrence(
id="internal-error",
createdAt=datetime(year=2023, month=3, day=3),
errorType="PythonException",
detail="You got me!!",
)

json_runner = decoy.mock(cls=protocol_runner.JsonRunner)

decoy.when(
Expand Down Expand Up @@ -199,7 +224,7 @@ async def test_analyze_updates_pending_on_error(
labware=[],
modules=[],
pipettes=[],
errors=[],
errors=[internal_error],
liquids=[],
),
)

0 comments on commit 2bbb9ba

Please sign in to comment.