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
Create a new file exceptions.py to define custom Python exceptions and the translate_exceptions decorator.
Implement the translate_exceptions decorator to catch C# exceptions and raise equivalent Python exceptions.
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
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
exceptions.py
to define custom Python exceptions and thetranslate_exceptions
decorator.translate_exceptions
decorator to catch C# exceptions and raise equivalent Python exceptions.Example implementation:
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