Improve error handling and reporting

This commit is contained in:
Valentijn 2018-02-06 17:44:41 +01:00
parent 2336e2bdb0
commit 7b1980a522

View file

@ -5,7 +5,12 @@ Requires "gi" package and "Gtk" & "Keybinder" modules.
# pylint: disable=broad-except
import sys
import threading
from clay.log import Logger
def report_error(error):
Logger.get().error("{0}: {1}".format(error.__class__.__name__, error))
IS_INIT = False
try:
# pylint: disable=import-error
import gi
@ -13,11 +18,15 @@ try:
gi.require_version('Gtk', '3.0') # noqa
from gi.repository import Keybinder, Gtk
# pylint: enable=import-error
except ImportError as error:
report_error(error)
ERROR_MESSAGE = "Couldn't import PyGObject"
except ValueError as error:
report_error(error)
ERROR_MESSAGE = "Couldn't find the Keybinder and/or Gtk modules"
except Exception as error:
sys.stderr.write('Could not import Keybinder and Gtk. Error was: "{}"\n'.format(error))
sys.stderr.write('Global shortcuts will not work.\n')
IS_INIT = False
ERROR = str(error)
report_error(error)
ERROR_MESSAGE = "There was unknown error: '{}'".format(error)
else:
IS_INIT = True
@ -54,10 +63,10 @@ class HotkeyManager(object):
threading.Thread(target=Gtk.main).start()
else:
NotificationArea.notify(
'Could not import Keybinder and Gtk. Error was: "{}"\n'
'Global shortcuts will not work.'.format(ERROR)
)
Logger.get().debug("Not loading the global shortcuts.")
NotificationArea.notify(ERROR_MESSAGE +
", this means the global shortcuts will not work.\n" +
"You can check the log for more details.")
@classmethod
def get(cls):