Except.py 1.59 KiB
# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import (
    QApplication, QMessageBox,
)

####################################
# Message Box for python exception #
####################################

def exception_message_box(exception):
    msg = QMessageBox()
    msg.setIcon(QMessageBox.Critical)

    msg.setText("Exception :")
    msg.setInformativeText(f"{exception}")
    msg.setWindowTitle("Exception")

    msg.exec_()

################
# Custom error #
################

class ExeceptionWithMessageBox(Exception):
    def __init__(self, title = "Exeception"):
        self.title = title

    def header(self):
        return "Generic error message"

    def short_message(self):
        return "Undefined error message"

    def message(self):
        return "Undefined error message"

    def alert(self):
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Critical)

        msg.setText(f"{self.header()} : {self.short_message()}")
        msg.setInformativeText(f"{self.message()}")
        msg.setWindowTitle(f"{self.title}")

        msg.exec_()


class FileFormatError(ExeceptionWithMessageBox):
    def __init__(self, filename, reason):
        super(FileFormatError, self).__init__(title = "FileFormatError")

        self.reason = reason
        self.filename = filename

    def __str__(self):
        return f"Invalid file format: '{self.filename}'\n{self.message()}"

    def header(self):
        return "File format error"

    def short_message(self):
        return "Invalid file format"

    def message(self):
        return f"Invalid file '{self.filename}' format because of '{self.reason}'"