diff --git a/src/ape_console/plugin.py b/src/ape_console/plugin.py index 60a22fcefa..baf07ca2f1 100644 --- a/src/ape_console/plugin.py +++ b/src/ape_console/plugin.py @@ -10,8 +10,9 @@ from ape._cli import cli from ape.exceptions import Abort, ApeException, handle_ape_exception from ape.logging import logger +from ape.managers import ProjectManager from ape.types import AddressType -from ape.utils import cached_property +from ape.utils import ManagerAccessMixin, cached_property @magics_class @@ -75,7 +76,15 @@ def bal(self, line: str = ""): def custom_exception_handler(self, etype, value, tb, tb_offset=None): - if not handle_ape_exception(value, [self.user_ns["project"].path]): + project = self.user_ns["project"] + if isinstance(project, ProjectManager): + path = project.path + else: + # This happens if assigned the variable `project` in your session + # to something other than ``ape.project``. + path = ManagerAccessMixin.project_manager.path + + if not handle_ape_exception(value, [path]): logger.error(Abort.from_ape_exception(value).format_message()) diff --git a/tests/functional/test_console.py b/tests/functional/test_console.py index 0c5af1cae5..0e46495b81 100644 --- a/tests/functional/test_console.py +++ b/tests/functional/test_console.py @@ -3,8 +3,9 @@ import pytest from ape import Project -from ape.utils import create_tempdir +from ape.utils import ManagerAccessMixin, create_tempdir from ape_console._cli import console +from ape_console.plugin import custom_exception_handler @pytest.fixture(autouse=True) @@ -51,3 +52,24 @@ def test_console_custom_project(mock_console, mock_ape_console_extras): # Ensure sys.path was updated correctly. assert sys.path[0] == str(project.path) + + +def test_custom_exception_handler_handles_non_ape_project(mocker): + """ + If the user has assigned the variable ``project`` to something else + in their active ``ape console`` session, the exception handler + **SHOULD NOT** attempt to use its ``.path``. + """ + session = mocker.MagicMock() + session.user_ns = {"project": 123} # Like doing `project = 123` in a console. + + err = Exception() + + handler_patch = mocker.patch("ape_console.plugin.handle_ape_exception") + + # Execute - this was failing before the fix. + custom_exception_handler(session, None, err, None) + + # We are expecting the local project's path in the handler. + expected_path = ManagerAccessMixin.project_manager.path + handler_patch.assert_called_once_with(err, [expected_path])