Hi,
I am pretty new to python and did some kind of return class which returns an outcome, user message and data when i call a method. I wonder if that is a common way of handling error/info/success messages. i add a simplified version of what i did.
I am pretty new to python and did some kind of return class which returns an outcome, user message and data when i call a method. I wonder if that is a common way of handling error/info/success messages. i add a simplified version of what i did.
class ResultCategories(enum.Enum): ERROR = enum.auto() WARNING = enum.auto() INFO = enum.auto() SUCCESS = enum.auto() class OperationResult: def __init__(self, category: ResultCategories, message: str | None = None, data: any = None) -> None: self.category = category # Type of outcome, as defined by ResultCategories. self.message = message # Descriptive message of the outcome. self.data = data # Data associated with the operation's outcome. class Messages: def __init__(self) -> None: self.current_priority = 0 def show_message(self, category: ResultCategories, message_text: str, message_priority: int) -> None: if message_priority >= self.current_priority: if isinstance(category, ResultCategories): # Show the message. Choose style based on category. self.current_priority = message_priority def clear_message(self) -> None: # Clear the message. self.current_priority = 0 def do_something(self, test: bool) -> OperationResult: if test is True: return OperationResult(ResultCategories.ERROR, "some error message") # Do something data_back = "abc" return OperationResult(ResultCategories.SUCCESS, "everything went great", data_back) messages = Messages() result: OperationResult = do_something(False) if result.category != ResultCategories.SUCCESS: messages.show_message(result.category, result.message, 50)Is that a solid solution?