Skip to content

Commit

Permalink
show exception popup on startup script errors
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b committed Feb 13, 2021
1 parent ab835ac commit 6ae1bf0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pythonhere/launcher_here.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[:]
Expand All @@ -43,3 +43,4 @@ def try_startup_script():
run_script(script)
except Exception:
Logger.exception("PythonHere: Error while starting script")
raise
14 changes: 12 additions & 2 deletions pythonhere/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
11 changes: 8 additions & 3 deletions tests/test_launcher_here.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from launcher_here import run_script, try_startup_script


Expand All @@ -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()


Expand All @@ -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()

Expand Down

0 comments on commit 6ae1bf0

Please sign in to comment.