diff --git a/app/config.py b/app/config.py index 5d5ef6c..431dce0 100644 --- a/app/config.py +++ b/app/config.py @@ -123,11 +123,11 @@ class Config(object): ROWS_FROM_ZERO = True SCROLL_TOP_MARGIN = 3 SECTION_ENDINGS = ("-", "+-", "-+") + SECTION_HEADER = "[Section header]" SECTION_STARTS = ("+", "+-", "-+") SONGFACTS_ON_NEXT = False START_GAP_WARNING_THRESHOLD = 300 SUBTOTAL_ON_ROW_ZERO = "[No subtotal on first row]" - TEXT_NO_TRACK_NO_NOTE = "[Section header]" TOD_TIME_FORMAT = "%H:%M:%S" TRACK_TIME_FORMAT = "%H:%M:%S" VLC_MAIN_PLAYER_NAME = "MusicMuster Main Player" diff --git a/app/log.py b/app/log.py index cbbef18..2adec79 100644 --- a/app/log.py +++ b/app/log.py @@ -6,16 +6,18 @@ import logging.config import logging.handlers import os import sys -from traceback import print_exception +import traceback import yaml # PyQt imports +from PyQt6.QtWidgets import QMessageBox # Third party imports import stackprinter # type: ignore # App imports from config import Config +from classes import ApplicationError class FunctionFilter(logging.Filter): @@ -76,26 +78,32 @@ with open("app/logging.yaml", "r") as f: log = logging.getLogger(Config.LOG_NAME) -def log_uncaught_exceptions(type_, value, traceback): - from helpers import send_mail +def handle_exception(exc_type, exc_value, exc_traceback): + if issubclass(exc_type, ApplicationError): + error = str(exc_value) + log.error(error) + QMessageBox.critical(None, "Application Error", error) + else: + # Handle unexpected errors (log and display) + error_msg = "".join(traceback.format_exception(exc_type, exc_value, exc_traceback)) + QMessageBox.critical(None, "Application Error", error_msg) - print("\033[1;31;47m") - print_exception(type_, value, traceback) - print("\033[1;37;40m") - print( - stackprinter.format( - value, suppressed_paths=["/pypoetry/virtualenvs/"], style="darkbg" - ) - ) - if os.environ["MM_ENV"] == "PRODUCTION": - msg = stackprinter.format(value) - send_mail( - Config.ERRORS_TO, - Config.ERRORS_FROM, - "Exception (log_uncaught_exceptions) from musicmuster", - msg, - ) - log.debug(msg) + print(stackprinter.format(exc_value, suppressed_paths=['/.venv'], style='darkbg')) + + msg = stackprinter.format(exc_value) + log.error(msg) + log.error(error_msg) + print("Critical error:", error_msg) # Consider logging instead of print + + if os.environ["MM_ENV"] == "PRODUCTION": + from helpers import send_mail + + send_mail( + Config.ERRORS_TO, + Config.ERRORS_FROM, + "Exception (log_uncaught_exceptions) from musicmuster", + msg, + ) -sys.excepthook = log_uncaught_exceptions +sys.excepthook = handle_exception diff --git a/app/musicmuster.py b/app/musicmuster.py index 61fe8bd..9df8eb2 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -2811,8 +2811,8 @@ if __name__ == "__main__": with db.Session() as session: update_bitrates(session) else: + app = QApplication(sys.argv) try: - app = QApplication(sys.argv) # PyQt6 defaults to a grey for labels palette = app.palette() palette.setColor( @@ -2830,6 +2830,7 @@ if __name__ == "__main__": win.show() status = app.exec() sys.exit(status) + except Exception as exc: if os.environ["MM_ENV"] == "PRODUCTION": from helpers import send_mail @@ -2843,10 +2844,8 @@ if __name__ == "__main__": ) log.debug(msg) else: - print("\033[1;31;47mUnhandled exception starts") print( stackprinter.format( exc, suppressed_paths=["/pypoetry/virtualenvs/"], style="darkbg" ) ) - print("Unhandled exception ends\033[1;37;40m") diff --git a/app/playlistmodel.py b/app/playlistmodel.py index 5571a1a..d7f3a46 100644 --- a/app/playlistmodel.py +++ b/app/playlistmodel.py @@ -1,5 +1,4 @@ # Standard library imports -# Allow forward reference to PlaylistModel from __future__ import annotations from operator import attrgetter