diff --git a/api/src/opentrons/protocol_api/core/engine/instrument.py b/api/src/opentrons/protocol_api/core/engine/instrument.py index 7f3460e37c1..7766bd0b8e2 100644 --- a/api/src/opentrons/protocol_api/core/engine/instrument.py +++ b/api/src/opentrons/protocol_api/core/engine/instrument.py @@ -31,6 +31,7 @@ from opentrons.protocol_engine.clients import SyncClient as EngineClient from opentrons.protocols.api_support.definitions import MAX_SUPPORTED_VERSION +from opentrons_shared_data.errors.exceptions import PipetteLiquidNotFoundError from opentrons_shared_data.pipette.dev_types import PipetteNameType from opentrons.protocol_api._nozzle_layout import NozzleLayout from opentrons.hardware_control.nozzle_manager import NozzleConfigurationType @@ -862,8 +863,8 @@ def find_liquid_level(self, well_core: WellCore) -> float: result is None ): # this should probably only happen in testing with mock components return 0 - # for general cases, result will either be a float > 0 or an error + # for general cases, result will either be a float > 0 or a PipetteLiquidNotFoundError try: return float(result.z_position) - except Exception: - raise BaseException(result) + except KeyError: + raise PipetteLiquidNotFoundError() diff --git a/api/src/opentrons/protocol_api/instrument_context.py b/api/src/opentrons/protocol_api/instrument_context.py index 4670852bb6f..1fa0d1ef329 100644 --- a/api/src/opentrons/protocol_api/instrument_context.py +++ b/api/src/opentrons/protocol_api/instrument_context.py @@ -6,6 +6,7 @@ from opentrons_shared_data.errors.exceptions import ( CommandPreconditionViolated, CommandParameterLimitViolated, + PipetteLiquidNotFoundError, UnexpectedTipRemovalError, ) from opentrons.protocol_engine.errors.exceptions import WellDoesNotExistError @@ -2055,14 +2056,14 @@ def detect_liquid(self, well: labware.Well) -> bool: :returns: A boolean. """ if well is None: - raise WellDoesNotExistError() + raise WellDoesNotExistError() from BaseException try: height = self._core.find_liquid_level(well._core) if height > 0: return True return False # it should never get here - except Exception: + except PipetteLiquidNotFoundError: return False @requires_version(2, 20) @@ -2072,11 +2073,11 @@ def require_liquid(self, well: labware.Well) -> None: :returns: None. """ if well is None: - raise WellDoesNotExistError() + raise WellDoesNotExistError("Well type was none.") try: self._core.find_liquid_level(well._core) - except Exception as e: + except PipetteLiquidNotFoundError as e: raise e @requires_version(2, 20) @@ -2091,5 +2092,5 @@ def get_liquid_height(self, well: labware.Well) -> float: try: height = self._core.find_liquid_level(well._core) return float(height) - except Exception: + except PipetteLiquidNotFoundError: return 0 diff --git a/api/tests/opentrons/protocol_api/test_instrument_context.py b/api/tests/opentrons/protocol_api/test_instrument_context.py index bb0bdc8b557..2215daede76 100644 --- a/api/tests/opentrons/protocol_api/test_instrument_context.py +++ b/api/tests/opentrons/protocol_api/test_instrument_context.py @@ -1,8 +1,6 @@ """Tests for the InstrumentContext public interface.""" from collections import OrderedDict import inspect - -from opentrons.protocol_engine.commands.pipetting_common import LiquidNotFoundError import pytest from pytest_lazyfixture import lazy_fixture # type: ignore[import-untyped] from decoy import Decoy @@ -40,6 +38,7 @@ from opentrons_shared_data.errors.exceptions import ( CommandPreconditionViolated, + PipetteLiquidNotFoundError, ) @@ -1281,7 +1280,7 @@ def test_detect_liquid( """It should only return booleans. Not raise an exception.""" mock_well = decoy.mock(cls=Well) decoy.when(mock_instrument_core.find_liquid_level(mock_well._core)).then_raise( - Exception(LiquidNotFoundError) + Exception(PipetteLiquidNotFoundError) ) result = subject.detect_liquid(mock_well) assert isinstance(result, bool) @@ -1297,17 +1296,17 @@ def test_require_liquid( """It should raise an exception when called on an.""" mock_well = decoy.mock(cls=Well) decoy.when(mock_instrument_core.find_liquid_level(mock_well._core)).then_raise( - Exception(LiquidNotFoundError) + Exception(PipetteLiquidNotFoundError) ) try: subject.require_liquid(mock_well) assert False - except Exception: + except PipetteLiquidNotFoundError: assert True @pytest.mark.parametrize("api_version", [APIVersion(2, 20)]) -def get_liquid_height( +def test_get_liquid_height( decoy: Decoy, mock_instrument_core: InstrumentCore, subject: InstrumentContext, @@ -1316,10 +1315,10 @@ def get_liquid_height( """It should return 0 on an empty well.""" mock_well = decoy.mock(cls=Well) decoy.when(mock_instrument_core.find_liquid_level(mock_well._core)).then_raise( - Exception(LiquidNotFoundError) + Exception(PipetteLiquidNotFoundError) ) try: result = subject.get_liquid_height(mock_well) assert False - except Exception: + except PipetteLiquidNotFoundError: assert result == 0