full exception handling

This commit is contained in:
BarsTiger
2022-05-18 22:18:10 +03:00
parent e4198f4fa0
commit b0bbf6021b
3 changed files with 43 additions and 10 deletions

View File

@@ -3,7 +3,9 @@ import sys
from PyQt5 import QtWidgets, QtCore from PyQt5 import QtWidgets, QtCore
from data.settings import Settings from data.settings import Settings
import pusher import pusher
import pusher.errors
import pysher import pysher
import modules.exception as exception
sys.path.append('gui') sys.path.append('gui')
# Importing the main window # Importing the main window
try: try:
@@ -13,6 +15,9 @@ except ImportError:
from gui.gui import Ui_MainWindow from gui.gui import Ui_MainWindow
from gui.functions import * from gui.functions import *
# Hooking exceptions
sys.excepthook = exception.hook
# Getting config # Getting config
settings = Settings.get_settings() settings = Settings.get_settings()
@@ -54,13 +59,7 @@ def initialize_pusher() -> None:
) )
receiver = pysher.Pusher(key=settings["key"], cluster=settings["cluster"]) receiver = pysher.Pusher(key=settings["key"], cluster=settings["cluster"])
receiver.connection.bind('pusher:connection_established', handle_connection_to_server) receiver.connection.bind('pusher:connection_established', handle_connection_to_server)
try: receiver.connect()
receiver.connect()
except Exception as e:
popup("Error", "Could not connect to pusher server\n"
"Do you have valid pusher config in settings tab?\n"
"Check full error message in console")
print(e)
def open_menu() -> None: def open_menu() -> None:
@@ -114,8 +113,23 @@ def handle_connection_to_server(connection) -> None:
""" """
print("Connected to server") print("Connected to server")
print("Server returned: " + str(connection)) print("Server returned: " + str(connection))
for client_id_av in list(client.channels_info(prefix_filter='admin-')['channels']): try:
print("Channel: " + client_id_av.split('-')[1]) for client_id_av in list(client.channels_info(prefix_filter='admin-')['channels']):
print("Channel: " + client_id_av.split('-')[1])
except pusher.errors.PusherBadRequest:
popup("Error", "Could not connect to pusher server\n"
"Do you have valid pusher config in settings tab?")
def reconnect_to_pusher() -> None:
"""
Reconnects to pusher
:return:
"""
global receiver
receiver.disconnect()
print("Disconnected from pusher")
initialize_pusher()
# Trying to connect to pusher # Trying to connect to pusher
@@ -123,7 +137,8 @@ initialize_pusher()
# Connecting user interface to functions # Connecting user interface to functions
ui.leftMenu.itemClicked.connect(lambda: handle_menu_click(ui.leftMenu.currentItem().text())) ui.leftMenu.itemClicked.connect(lambda: handle_menu_click(ui.leftMenu.currentItem().text()))
ui.saveSettingsButton.clicked.connect(lambda: update_settings(ui)) ui.saveSettingsButton.clicked.connect(lambda: (globals().update(settings=update_settings(ui))))
ui.reconRefreshButton.clicked.connect(lambda: reconnect_to_pusher())
# Handling closing of the window to exit whole program # Handling closing of the window to exit whole program
sys.exit(app.exec_()) sys.exit(app.exec_())

View File

@@ -1,5 +1,6 @@
from data.settings import Settings from data.settings import Settings
import ctypes import ctypes
from ezzthread import threaded
def popup(title, text, style=0): def popup(title, text, style=0):
@@ -16,6 +17,11 @@ def popup(title, text, style=0):
return ctypes.windll.user32.MessageBoxW(0, text, title, style) return ctypes.windll.user32.MessageBoxW(0, text, title, style)
@threaded
def t_popup(title, text, style=0):
popup(title, text, style)
def fill_settings(ui) -> None: def fill_settings(ui) -> None:
settings = Settings.get_settings() settings = Settings.get_settings()
ui.chooseAnimationBox.setCurrentText(settings.get("animation")) ui.chooseAnimationBox.setCurrentText(settings.get("animation"))
@@ -38,3 +44,4 @@ def update_settings(ui) -> None:
"client_id": get_text(ui.imgurClientId.text().strip()) "client_id": get_text(ui.imgurClientId.text().strip())
} }
list(map((lambda x: Settings.update(x, settings[x])), settings)) list(map((lambda x: Settings.update(x, settings[x])), settings))
return Settings.get_settings()

View File

@@ -0,0 +1,11 @@
from gui.functions import t_popup
from traceback import format_exception
def hook(type_, value, traceback):
t_popup("Unhandled exception", "An unhandled exception has occurred, \n"
"please check the console for more information.")
print("[!] Error happened")
print("Error type: ", type_.__name__)
print("Error value: ", value)
print("Error traceback: ", format_exception(type_, value, traceback)[2])