From 6ae1bf0fbfa741f8cda3ae4927fd0420cc4490ba Mon Sep 17 00:00:00 2001 From: b3b Date: Sat, 13 Feb 2021 16:59:31 +0300 Subject: [PATCH] show exception popup on startup script errors --- pythonhere/launcher_here.py | 3 ++- pythonhere/main.py | 14 ++++++++++++-- tests/test_launcher_here.py | 11 ++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/pythonhere/launcher_here.py b/pythonhere/launcher_here.py index 78f1301..083358c 100644 --- a/pythonhere/launcher_here.py +++ b/pythonhere/launcher_here.py @@ -16,7 +16,7 @@ def run_script(script: str): path = Path(script).resolve(strict=True) except FileNotFoundError: Logger.error("Script not found: %s", script) - return + raise Exception(f"Script not found: {script}") from None original_cwd = str(Path.cwd()) original_sys_path = sys.path[:] @@ -43,3 +43,4 @@ def try_startup_script(): run_script(script) except Exception: Logger.exception("PythonHere: Error while starting script") + raise diff --git a/pythonhere/main.py b/pythonhere/main.py index 92ddf97..1c95e84 100644 --- a/pythonhere/main.py +++ b/pythonhere/main.py @@ -3,7 +3,12 @@ from launcher_here import try_startup_script -try_startup_script() # run script entrypoint, if it was passed +try: + try_startup_script() # run script entrypoint, if it was passed +except Exception as exc: + startup_script_exception = exc # pylint: disable=invalid-name +else: + startup_script_exception = None # pylint: disable=invalid-name import asyncio import os @@ -13,11 +18,12 @@ from typing import Any, Dict from kivy.app import App +from kivy.clock import Clock from kivy.config import Config, ConfigParser from kivy.logger import Logger from enum_here import ScreenName, ServerState -from exception_manager_here import install_exception_handler +from exception_manager_here import install_exception_handler, show_exception_popup from patches_here import monkeypatch_kivy from server_here import run_ssh_server from window_here import reset_window_environment @@ -71,6 +77,10 @@ def build(self): } ) self.update_server_config_status() + if startup_script_exception: + Clock.schedule_once( + lambda _: show_exception_popup(startup_script_exception), 0 + ) def run_app(self): """Run application and SSH server tasks.""" diff --git a/tests/test_launcher_here.py b/tests/test_launcher_here.py index ffbee3e..9a8eed8 100644 --- a/tests/test_launcher_here.py +++ b/tests/test_launcher_here.py @@ -1,3 +1,4 @@ +import pytest from launcher_here import run_script, try_startup_script @@ -9,7 +10,8 @@ def test_run_script(mocker, test_py_script): def test_run_script_not_found(mocker): run_path = mocker.patch("runpy.run_path") - run_script("not_exist.py") + with pytest.raises(Exception, match="Script not found"): + run_script("not_exist.py") run_path.assert_not_called() @@ -30,9 +32,12 @@ def test_try_startup_script(mocker, mocked_android_modules): def test_try_startup_exception(mocker, mocked_android_modules): mocker.patch("launcher_here.platform", "android") logger_exception = mocker.patch("launcher_here.Logger.exception") - run_script = mocker.patch("launcher_here.run_script", side_effect=Exception()) + run_script = mocker.patch("launcher_here.run_script", + side_effect=Exception("test")) + + with pytest.raises(Exception, match="test"): + try_startup_script() - try_startup_script() run_script.assert_called_once() logger_exception.assert_called_once()