-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexception_manager_here.py
More file actions
68 lines (49 loc) · 1.9 KB
/
exception_manager_here.py
File metadata and controls
68 lines (49 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""App exceptions manager."""
import asyncio
import traceback
from pathlib import Path
from kivy.base import (
ExceptionHandler,
ExceptionManager,
)
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.logger import Logger
from kivy.properties import StringProperty # pylint: disable=no-name-in-module
from kivy.uix.popup import Popup
def load_exception_popup_style():
"""Load KV rules for `UnhandledExceptionPopupHere`."""
kv_path = str(Path(__file__).with_suffix(".kv"))
if kv_path not in Builder.files:
Builder.load_file(kv_path)
class ErrorMessageOnException(ExceptionHandler):
"""Handler that catches App exceptions, and show error message with details."""
def handle_exception(self, exception) -> int:
"""Handle a exception."""
if isinstance(exception, (asyncio.CancelledError, KeyboardInterrupt)):
return ExceptionManager.RAISE
Logger.exception("Unhandled Exception catched")
show_exception_popup()
return ExceptionManager.PASS
class UnhandledExceptionPopupHere(Popup):
"""Popup with details about exception."""
message = StringProperty("")
def install_exception_handler():
"""Install `ErrorMessageOnException` exception handler."""
if not any(
isinstance(handler, ErrorMessageOnException)
for handler in ExceptionManager.handlers
):
ExceptionManager.add_handler(ErrorMessageOnException())
def show_exception_popup(exc: Exception | None = None):
"""Show exception popup."""
load_exception_popup_style()
if exc:
message = "".join(traceback.format_exception(type(exc), exc, exc.__traceback__))
else:
message = traceback.format_exc()
popup = UnhandledExceptionPopupHere(message=message)
popup.open()
def reset_cursor(_):
popup.ids.catched_exception_code_input_here.cursor = (0, 0)
Clock.schedule_once(reset_cursor)