Skip to content

Commit

Permalink
Using notify for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fdev31 committed Nov 13, 2023
1 parent b31d467 commit b74a081
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Host process for multiple Hyprland plugins, such as:

Check the [Getting started](https://github.com/hyprland-community/pyprland/wiki/Getting-started) page and the [plugins list](https://github.com/hyprland-community/pyprland/wiki/Plugins), finally browse the [wiki](https://github.com/hyprland-community/pyprland/wiki) for more information.

# WIP

- Using hyprland's notifications in case of serious errors

# 1.5.0

- Add support for a [TOML](https://toml.io/) configuration file, will be used if found (instead of JSON)
Expand Down
18 changes: 17 additions & 1 deletion pyprland/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from collections import defaultdict

from .common import PyprError, get_logger, init_logger
from .ipc import get_event_stream
from .ipc import get_event_stream, notify_error, notify_fatal, notify_info
from .ipc import init as ipc_init
from .plugins.interface import Plugin

Expand Down Expand Up @@ -77,8 +77,12 @@ async def load_config(self, init=True):
self.plugins[name] = plug
except ModuleNotFoundError:
self.log.error("Unable to locate plugin called '%s'", name)
await notify_info(
f'Config requires plugin "{name}" but pypr can\'t find it'
)
continue
except Exception as e:
await notify_info(f"Error loading plugin {name}: {e}")
self.log.error("Error loading plugin %s:", name, exc_info=True)
raise PyprError() from e
if init:
Expand All @@ -87,6 +91,7 @@ async def load_config(self, init=True):
except PyprError:
raise
except Exception as e:
await notify_info(f"Error initializing plugin {name}: {e}")
self.log.error("Error initializing plugin %s:", name, exc_info=True)
raise PyprError() from e

Expand All @@ -103,11 +108,15 @@ async def _callHandler(self, full_name, *params):
"Bug detected, please report on https://github.com/fdev31/pyprland/issues"
)
self.log.exception(e)
await notify_error(
f"Pypr integrity check failed on {plugin.name}::{full_name}: {e}"
)
except Exception as e: # pylint: disable=W0718
self.log.warning(
"%s::%s(%s) failed:", plugin.name, full_name, params
)
self.log.exception(e)
await notify_error(f"Pypr error {plugin.name}::{full_name}: {e}")
break
else:
return False
Expand Down Expand Up @@ -206,9 +215,15 @@ async def run_daemon():
try:
await manager.load_config() # ensure sockets are connected first
except PyprError as e:
if bool(str(e)):
await notify_fatal(f"Pypr failed to start: {e}")
else:
await notify_fatal("Pypr failed to start!")

raise SystemExit(1) from e
except Exception as e:
manager.log.critical("Failed to load config.", exc_info=True)
await notify_fatal(f"Pypr couldn't load config: {e}")
raise SystemExit(1) from e

try:
Expand Down Expand Up @@ -253,6 +268,7 @@ async def run_client():
_, writer = await asyncio.open_unix_connection(CONTROL)
except (ConnectionRefusedError, FileNotFoundError) as e:
manager.log.critical("Failed to open control socket, is pypr daemon running ?")
await notify_error("Pypr can't connect, is daemon running ?")
raise PyprError() from e

writer.write((" ".join(sys.argv[1:])).encode())
Expand Down
13 changes: 12 additions & 1 deletion pyprland/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import asyncio
import json
import os
from logging import Logger
from typing import Any
from logging import Logger
from functools import partial

from .common import PyprError, get_logger

Expand All @@ -14,6 +15,16 @@
EVENTS = f'/tmp/hypr/{ os.environ["HYPRLAND_INSTANCE_SIGNATURE"] }/.socket2.sock'


async def notify(text, duration=3, color="ff1010", icon=-1):
"Uses hyprland notification system"
await hyprctl(f"{icon} {int(duration*1000)} rgb({color}) {text}", "notify")


notify_fatal = partial(notify, icon=3, duration=10)
notify_error = partial(notify, icon=1, duration=5)
notify_info = partial(notify, icon=2, duration=5)


async def get_event_stream():
"Returns a new event socket connection"
return await asyncio.open_unix_connection(EVENTS)
Expand Down

0 comments on commit b74a081

Please sign in to comment.