@xinya.zhu @simon.zhao
Thanks for your replies and willingness to look into this.
I would ask for one thing specific to the Python Exception interface (this may also apply to .NET but it’s not my primary area of expertise) of Cells. It would be very helpful for the interface to raise a different exception class for each underlying Exception Type. This works more naturally with how Python developers expect to work with exceptions and results in cleaner code.
If the resulting interface mirrors the current .NET interface I would need to handle exceptions like this:
try:
# Do Something in Cells
pass
except CellsException as e:
if e.code == ExceptionType.Formula:
# Handle formula error
pass
elif e.code == ExceptionType.InvalidData:
# Handle data error
pass
else:
# Not something we can deal with here, probably a showstopper.
# Forgetting to do this will hide errors!
raise e
Ideally you’d want to write your exception handler like this:
try:
# Do Something in Cells
pass
except CellsExceptionFormula as e:
# Handle formula error
pass
except CellsExceptionInvalidData as e:
# Handle data error
pass
The advantage of the second is that while it’s cleaner it there is also no risk that I forget to reraise an unhandled exception type and swallow the error unintentionally.
Internally in the Cells Python code, once you have the value of ExceptionType and the message it’s pretty easy to setup a map to give you the correct Exception object type to raise.
Something like this:
class _CellsException(Exception):
code = None
def __init__(self, message: str):
super().__init__(message)
@property
def message(self):
return self.args[0]
@property
def code(self):
return self.code
_exception_map = {}
# Create and raise exception
def raise_cell_exception(code: aspose.cells.ExceptionType, message: str):
klass = _exception_map[code]
raise klass(message)
# Creat Exception classes from Exception Types
for _e in aspose.cells.ExceptionType:
#Build CamelCase name
name = "Cells" + _e.name.title().replace("_", "") + "Error"
# Create new class with correct value for klass.code.
klass = type(name, (_CellsException,), dict(code=_e))
# Add to module namespace for code completion, etc.
globals()[name] = klass
# Add to map for simple lookup later
_exception_map[_e] = klass
Whenever you have an exception to raise from the backend you can do this:
>>> raise_cell_exception(aspose.cells.ExceptionType.CHART, "test")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in raise_cell_exception
CellsChartError: 'test'
Thanks again for looking at this. This will make it easier for me to justify updating our Cells license to my manager.