amruthvvkp / flaui-uiautomation-wrapper

Tool to perform UI Automation on Windows desktop applications using an underlying Fla-UI wrapper.
GNU General Public License v3.0
6 stars 3 forks source link

Implement translate_exceptions for Pythonic exception handling #73

Open amruthvvkp opened 2 months ago

amruthvvkp commented 2 months ago

Is your feature request related to a problem? Please describe. We need to implement a translate_exceptions mechanism in our Python wrapper for FlaUI to convert C# exceptions into more Pythonic exceptions. This will improve the user experience for Python developers using our library and make error handling more intuitive.

Describe the solution you'd like

  1. Create a new file exceptions.py to define custom Python exceptions and the translate_exceptions decorator.
  2. Implement the translate_exceptions decorator to catch C# exceptions and raise equivalent Python exceptions.
  3. Apply the decorator to relevant methods in the wrapper classes.

Example implementation:

# flaui/exceptions.py

from functools import wraps
from System import InvalidOperationException, ArgumentException  # Import C# exceptions

class InvalidOperationError(Exception):
    pass

class ArgumentError(Exception):
    pass

def translate_exceptions(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except InvalidOperationException as e:
            raise InvalidOperationError(str(e)) from None
        except ArgumentException as e:
            raise ArgumentError(str(e)) from None
        # Add more exception translations as needed
    return wrapper
Usage example in wrapper classes:

# flaui/core/application.py

from flaui.exceptions import translate_exceptions, InvalidOperationError, ArgumentError

class Application:
    def __init__(self, automation):
        self._automation = automation
        self._app = None  # This will hold the C# Application object

    @property
    @translate_exceptions
    def exit_code(self):
        return self._app.ExitCode

    @translate_exceptions
    def some_method(self, arg):
        return self._app.SomeMethod(arg)

    # Other methods...
Alternative implementation using a wrapper method:

# flaui/core/application.py

from System import InvalidOperationException, ArgumentException
from flaui.exceptions import InvalidOperationError, ArgumentError

class Application:
    def __init__(self, automation):
        self._automation = automation
        self._app = None  # This will hold the C# Application object

    def _call_cs_method(self, method, *args, **kwargs):
        try:
            return method(*args, **kwargs)
        except InvalidOperationException as e:
            raise InvalidOperationError(str(e)) from None
        except ArgumentException as e:
            raise ArgumentError(str(e)) from None
        # Add more exception translations as needed

    @property
    def exit_code(self):
        return self._call_cs_method(lambda: self._app.ExitCode)

    def some_method(self, arg):
        return self._call_cs_method(self._app.SomeMethod, arg)

    # Other methods...

Tasks

[ ] Create exceptions.py file [ ] Implement translate_exceptions decorator [ ] Define custom Python exceptions [ ] Apply translate_exceptions to relevant methods in wrapper classes [ ] Update documentation to reflect new exception handling [ ] Add unit tests for exception translation [ ] Review and update all C# exception occurrences in the codebase