Improve ApplicationError reporting

This commit is contained in:
Keith Edmunds 2025-03-07 15:44:21 +00:00
parent ccc1737f2d
commit 6dd34b292f
4 changed files with 32 additions and 26 deletions

View File

@ -123,11 +123,11 @@ class Config(object):
ROWS_FROM_ZERO = True ROWS_FROM_ZERO = True
SCROLL_TOP_MARGIN = 3 SCROLL_TOP_MARGIN = 3
SECTION_ENDINGS = ("-", "+-", "-+") SECTION_ENDINGS = ("-", "+-", "-+")
SECTION_HEADER = "[Section header]"
SECTION_STARTS = ("+", "+-", "-+") SECTION_STARTS = ("+", "+-", "-+")
SONGFACTS_ON_NEXT = False SONGFACTS_ON_NEXT = False
START_GAP_WARNING_THRESHOLD = 300 START_GAP_WARNING_THRESHOLD = 300
SUBTOTAL_ON_ROW_ZERO = "[No subtotal on first row]" SUBTOTAL_ON_ROW_ZERO = "[No subtotal on first row]"
TEXT_NO_TRACK_NO_NOTE = "[Section header]"
TOD_TIME_FORMAT = "%H:%M:%S" TOD_TIME_FORMAT = "%H:%M:%S"
TRACK_TIME_FORMAT = "%H:%M:%S" TRACK_TIME_FORMAT = "%H:%M:%S"
VLC_MAIN_PLAYER_NAME = "MusicMuster Main Player" VLC_MAIN_PLAYER_NAME = "MusicMuster Main Player"

View File

@ -6,16 +6,18 @@ import logging.config
import logging.handlers import logging.handlers
import os import os
import sys import sys
from traceback import print_exception import traceback
import yaml import yaml
# PyQt imports # PyQt imports
from PyQt6.QtWidgets import QMessageBox
# Third party imports # Third party imports
import stackprinter # type: ignore import stackprinter # type: ignore
# App imports # App imports
from config import Config from config import Config
from classes import ApplicationError
class FunctionFilter(logging.Filter): class FunctionFilter(logging.Filter):
@ -76,26 +78,32 @@ with open("app/logging.yaml", "r") as f:
log = logging.getLogger(Config.LOG_NAME) log = logging.getLogger(Config.LOG_NAME)
def log_uncaught_exceptions(type_, value, traceback): def handle_exception(exc_type, exc_value, exc_traceback):
from helpers import send_mail 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(stackprinter.format(exc_value, suppressed_paths=['/.venv'], style='darkbg'))
print_exception(type_, value, traceback)
print("\033[1;37;40m") msg = stackprinter.format(exc_value)
print( log.error(msg)
stackprinter.format( log.error(error_msg)
value, suppressed_paths=["/pypoetry/virtualenvs/"], style="darkbg" print("Critical error:", error_msg) # Consider logging instead of print
)
) if os.environ["MM_ENV"] == "PRODUCTION":
if os.environ["MM_ENV"] == "PRODUCTION": from helpers import send_mail
msg = stackprinter.format(value)
send_mail( send_mail(
Config.ERRORS_TO, Config.ERRORS_TO,
Config.ERRORS_FROM, Config.ERRORS_FROM,
"Exception (log_uncaught_exceptions) from musicmuster", "Exception (log_uncaught_exceptions) from musicmuster",
msg, msg,
) )
log.debug(msg)
sys.excepthook = log_uncaught_exceptions sys.excepthook = handle_exception

View File

@ -2811,8 +2811,8 @@ if __name__ == "__main__":
with db.Session() as session: with db.Session() as session:
update_bitrates(session) update_bitrates(session)
else: else:
app = QApplication(sys.argv)
try: try:
app = QApplication(sys.argv)
# PyQt6 defaults to a grey for labels # PyQt6 defaults to a grey for labels
palette = app.palette() palette = app.palette()
palette.setColor( palette.setColor(
@ -2830,6 +2830,7 @@ if __name__ == "__main__":
win.show() win.show()
status = app.exec() status = app.exec()
sys.exit(status) sys.exit(status)
except Exception as exc: except Exception as exc:
if os.environ["MM_ENV"] == "PRODUCTION": if os.environ["MM_ENV"] == "PRODUCTION":
from helpers import send_mail from helpers import send_mail
@ -2843,10 +2844,8 @@ if __name__ == "__main__":
) )
log.debug(msg) log.debug(msg)
else: else:
print("\033[1;31;47mUnhandled exception starts")
print( print(
stackprinter.format( stackprinter.format(
exc, suppressed_paths=["/pypoetry/virtualenvs/"], style="darkbg" exc, suppressed_paths=["/pypoetry/virtualenvs/"], style="darkbg"
) )
) )
print("Unhandled exception ends\033[1;37;40m")

View File

@ -1,5 +1,4 @@
# Standard library imports # Standard library imports
# Allow forward reference to PlaylistModel
from __future__ import annotations from __future__ import annotations
from operator import attrgetter from operator import attrgetter