Skip to content

Error Handler

Squid Coder edited this page Nov 10, 2024 · 1 revision

ErrorHandler Documentation

Overview

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.

Classes

1. ErrorHandler.GenericError

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.

2. ErrorHandler.MismatchedSettingsError

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.

3. ErrorHandler.LanguageLabelGenericError

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.

4. ErrorHandler.LanguageLabelMismatchError

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.

5. ErrorHandler.GenericWarning

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.

6. ErrorHandler.SettingsWarning

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.

Methods

1. ErrorHandler.show_error_popup

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").

2. ErrorHandler.show_warning_popup

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").

3. ErrorHandler.show_cancelable_warning_popup

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:

    • True or False depending on whether the user accepted or canceled the warning.

4. ErrorHandler.handle_exception

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).

5. ErrorHandler.handle_warning

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.

Usage Example

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.")

Global Handlers Setup

The ErrorHandler module automatically sets up global handlers for uncaught exceptions and warnings:

sys.excepthook = ErrorHandler.handle_exception
warnings.showwarning = ErrorHandler.handle_warning

This ensures that whenever an uncaught exception or warning occurs, the custom handlers defined in ErrorHandler are invoked.