invoice2data
invoice2data copied to clipboard
Improve exception handling
I'm using invoice2data as a python lib in the Odoo module account_invoice_import_invoice2data, cf https://github.com/OCA/edi/blob/10.0/account_invoice_import_invoice2data/wizard/account_invoice_import.py#L54
When the extraction fails because
- some fields didn't match
- or because no template was found
then the extract_data method of the lib just returns nothing and the reason of the failure is written in the logs.
In order to display an accurate error message to the user of Odoo, writing the reason of the failure in the logs is not enough, I think we need proper exceptions with nice error messages in exceptions so that I can catch these exceptions and display a good error message.
Agreed. How do you usually deal with this? Can Python return a structured error message, like a dict? We do tell the user when a specific field fails, but it's not handled centrally or in a structured way. That could be improved. Let's collect some goals and plan first.
I don't have a lot of experience with python exceptions, so I may not be the best person to advise on how we should design the exceptions for invoice2data. @hbrunn could you give us some advice on this ? As you have a great Python+Odoo experience and you already contributed to this project, your input would be very appreciated.
I'd start by declaring some base class deriving from ValueError (or some other standard exception), and simply add a constructor that allows you to pass whatever data you need to pass. Exceptions are just class instances, so you can add whatever you like. cf https://docs.python.org/2/library/exceptions.html#exceptions.UnicodeError for an example in the standard lib
PS: Don't forget to override __str__
to print something sensible using those values
Anyone working on this? If yes, please assign yourself to this issue, so it's "reserved".
I agree with @alexis-via on this one. Some additional error message is very welcomed. Even a function that when for instace one input parser fails (e.g. pdftotext). Odoo provides the user with the option to use another parser (tesseract).
@rmilecki Any input on this? Are you using invoice2data as a lib?
I use it as a lib but whenever something goes wrong, I have to use CLI to debug. So I'm all for adding some better errors handling.