jmcnamara / XlsxWriter

A Python module for creating Excel XLSX files.
https://xlsxwriter.readthedocs.io
BSD 2-Clause "Simplified" License
3.66k stars 632 forks source link

RFC: Refactoring of return values to Enums or Exceptions #1100

Open jmcnamara opened 1 week ago

jmcnamara commented 1 week ago

Request for Comment

The underlying design for error handling in XlsxWriter is that actions that could cause file corruption or an Excel error (such as duplicate worksheet names) are raised as exceptions. Other "recoverable" actions such as writing to a cell beyond the Excel range are ignored and a negative return value is returned along with a warning. The rationale for return codes/warnings was to allow the overall file creation to complete for more harmless input/parameter errors.

In pratice the return code approach isn't useful for folks who want to handle them programattically due to two issues: the first is that the codes aren't obvioius (-1, -2, etc.) and the second is that they don't correspond to the standard Python exception handling idiom.

As part of the XlsxWriter v2 refactoring/improvements I would like to refactor/improve this in one of two ways:

I am looking for opinions on this and you can weigh in below if you wish.

See also the previous discussion on this at #892

wilcoxon commented 1 week ago

Personally, I strongly prefer return IntEnum. I'm not a fan of raising Exceptions for non-fatal errors.

ijustlovemath commented 1 week ago

I think the principle of least surprise would dictate exceptions, but that's just me. Raising and handling exceptions is about as Pythonic as it gets (see: how for loops actually work)