-
Notifications
You must be signed in to change notification settings - Fork 0
Error Handler
The ErrorHandler module in SquidLibs provides a custom error and warning handling system for your application. It offers custom exception classes, warning classes, and methods to display error/warning popups. The module also sets up global handlers for exceptions and warnings, making it easy to manage errors and warnings in your application in a consistent way.
The base class for all custom errors. It inherits from Python’s built-in Exception class.
-
Constructor:
-
__init__(self, message): Initializes the exception with a custom message.
-
-
String Representation:
-
__str__(self): Returns the message of the exception.
-
Inherits from GenericError, this class is used when settings do not match the expected configuration.
-
Constructor:
-
__init__(self, message, mismatchedSetting): Initializes the exception with a custom message and the setting that caused the mismatch.
-
Inherits from GenericError, this class is used when there is a general language label issue.
-
Constructor:
-
__init__(self, message): Initializes the exception with a custom message.
-
Inherits from MismatchedSettingsError, this class specifically handles errors related to mismatched language labels when loading translation files.
-
Constructor:
-
__init__(self, lang_code, found_lang): Initializes the exception with the expected and found language codes.
-
The base class for custom warnings, inheriting from Python’s built-in Warning class.
-
Constructor:
-
__init__(self, message): Initializes the warning with a custom message.
-
-
String Representation:
-
__str__(self): Returns the message of the warning.
-
Inherits from GenericWarning, this class is used when there is a warning related to mismatched settings.
-
Constructor:
-
__init__(self, setting_name, message): Initializes the warning with the setting name and a custom message.
-
Displays a popup with an error message.
-
Parameters:
-
error_msg: The error message to display. -
title(optional): The title of the error popup (default is "Error").
-
Displays a popup with a warning message.
-
Parameters:
-
warning_msg: The warning message to display. -
title(optional): The title of the warning popup (default is "Warning").
-
Displays a warning popup with a "Cancel" button. Returns True if the user accepts, False if they cancel.
-
Parameters:
-
warning_msg: The warning message to display. -
title(optional): The title of the warning popup (default is "Warning").
-
-
Returns:
-
TrueorFalsedepending on whether the user accepted or canceled the warning.
-
This method is set as the global exception handler. It is called whenever an uncaught exception occurs.
-
Parameters:
-
exc_type: The type of the exception. -
exc_value: The exception instance. -
exc_traceback: The traceback of the exception.
-
Displays an error popup with the exception's message and traceback (if in debug mode).
This method is set as the global warning handler. It is called whenever a warning is triggered.
-
Parameters:
-
message: The warning message. -
category: The warning category (e.g.,UserWarning). -
filename: The name of the file where the warning originated. -
lineno: The line number where the warning originated. -
file(optional): A file object to write the warning to. -
line(optional): The line of source code that triggered the warning.
-
Displays a warning popup with the warning message.
Here’s an example of how to use the ErrorHandler in your code:
from SquidLibs import ErrorHandler
try:
# Your code that might raise an exception
raise ErrorHandler.GenericError("Something went wrong!")
except ErrorHandler.GenericError as e:
ErrorHandler.show_error_popup(str(e))
# Displaying a warning
ErrorHandler.show_warning_popup("This is a warning message.")The ErrorHandler module automatically sets up global handlers for uncaught exceptions and warnings:
sys.excepthook = ErrorHandler.handle_exception
warnings.showwarning = ErrorHandler.handle_warningThis ensures that whenever an uncaught exception or warning occurs, the custom handlers defined in ErrorHandler are invoked.