Skip to content

Commit

Permalink
Introduce error types
Browse files Browse the repository at this point in the history
Related #2273
  • Loading branch information
MattHag authored and pfps committed Jan 2, 2025
1 parent 3bf8a85 commit ab52c4a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
10 changes: 6 additions & 4 deletions lib/solaar/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from collections import namedtuple
from functools import partial
from typing import Callable

import gi
import logitech_receiver
Expand All @@ -38,6 +39,7 @@
from . import configuration
from . import dbus
from . import i18n
from .ui import common

if typing.TYPE_CHECKING:
from hidapi.common import DeviceInfo
Expand Down Expand Up @@ -345,7 +347,7 @@ def ping_all(resuming=False):
_error_callback = None # GUI callback to report errors


def setup_scanner(status_changed_callback, setting_changed_callback, error_callback):
def setup_scanner(status_changed_callback: Callable, setting_changed_callback: Callable, error_callback: Callable):
global _status_callback, _error_callback, _setting_callback
assert _status_callback is None, "scanner was already set-up"
_status_callback = status_changed_callback
Expand All @@ -368,11 +370,11 @@ def _process_add(device_info: DeviceInfo, retry):
if retry:
GLib.timeout_add(2000.0, _process_add, device_info, retry - 1)
else:
_error_callback("permissions", device_info.path)
_error_callback(common.ErrorReason.PERMISSIONS, device_info.path)
else:
_error_callback("nodevice", device_info.path)
_error_callback(common.ErrorReason.NO_DEVICE, device_info.path)
except exceptions.NoReceiver:
_error_callback("nodevice", device_info.path)
_error_callback(common.ErrorReason.NO_DEVICE, device_info.path)


# receiver add/remove events will start/stop listener threads
Expand Down
4 changes: 2 additions & 2 deletions lib/solaar/ui/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
from gi.repository import Gtk

from solaar.i18n import _
from solaar.ui import common

from . import pair_window
from .common import error_dialog


def make_image_menu_item(label, icon_name, function, *args):
Expand Down Expand Up @@ -95,4 +95,4 @@ def unpair(window, device):
try:
del receiver[device_number]
except Exception:
error_dialog("unpair", device)
common.error_dialog(common.ErrorReason.UNPAIR, device)
23 changes: 15 additions & 8 deletions lib/solaar/ui/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import logging

from enum import Enum
from typing import Tuple

import gi
Expand All @@ -30,22 +31,28 @@
logger = logging.getLogger(__name__)


def _create_error_text(reason: str, object_) -> Tuple[str, str]:
if reason == "permissions":
class ErrorReason(Enum):
PERMISSIONS = "Permissions"
NO_DEVICE = "No device"
UNPAIR = "Unpair"


def _create_error_text(reason: ErrorReason, object_) -> Tuple[str, str]:
if reason == ErrorReason.PERMISSIONS:
title = _("Permissions error")
text = (
_("Found a Logitech receiver or device (%s), but did not have permission to open it.") % object_
+ "\n\n"
+ _("If you've just installed Solaar, try disconnecting the receiver or device and then reconnecting it.")
)
elif reason == "nodevice":
elif reason == ErrorReason.NO_DEVICE:
title = _("Cannot connect to device error")
text = (
_("Found a Logitech receiver or device at %s, but encountered an error connecting to it.") % object_
+ "\n\n"
+ _("Try disconnecting the device and then reconnecting it or turning it off and then on.")
)
elif reason == "unpair":
elif reason == ErrorReason.UNPAIR:
title = _("Unpairing failed")
text = (
_("Failed to unpair %{device} from %{receiver}.").format(
Expand All @@ -56,11 +63,11 @@ def _create_error_text(reason: str, object_) -> Tuple[str, str]:
+ _("The receiver returned an error, with no further details.")
)
else:
raise Exception("ui.error_dialog: don't know how to handle (%s, %s)", reason, object_)
raise Exception("ui.error_dialog: don't know how to handle (%s, %s)", reason.name, object_)
return title, text


def _error_dialog(reason: str, object_):
def _error_dialog(reason: ErrorReason, object_):
logger.error("error: %s %s", reason, object_)
title, text = _create_error_text(reason, object_)

Expand All @@ -70,8 +77,7 @@ def _error_dialog(reason: str, object_):
m.destroy()


def error_dialog(reason, object_):
assert reason is not None
def error_dialog(reason: ErrorReason, object_):
GLib.idle_add(_error_dialog, reason, object_)


Expand All @@ -91,5 +97,6 @@ def stop_async():


def ui_async(function, *args, **kwargs):
"""Runs a function asynchronously."""
if _task_runner:
_task_runner(function, *args, **kwargs)
6 changes: 3 additions & 3 deletions tests/solaar/ui/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"reason, expected_in_title, expected_in_text",
[
(
"permissions",
common.ErrorReason.PERMISSIONS,
"Permissions error",
"not have permission to open",
),
("nodevice", "connect to device error", "error connecting"),
("unpair", "Unpairing failed", "receiver returned an error"),
(common.ErrorReason.NO_DEVICE, "connect to device error", "error connecting"),
(common.ErrorReason.UNPAIR, "Unpairing failed", "receiver returned an error"),
],
)
def test_create_error_text(reason, expected_in_title, expected_in_text):
Expand Down

0 comments on commit ab52c4a

Please sign in to comment.