invoice2data icon indicating copy to clipboard operation
invoice2data copied to clipboard

Improve exception handling

Open alexis-via opened this issue 5 years ago • 7 comments

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

  1. some fields didn't match
  2. 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.

alexis-via avatar Jan 08 '19 11:01 alexis-via

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.

m3nu avatar Jan 08 '19 11:01 m3nu

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.

alexis-via avatar Jan 08 '19 14:01 alexis-via

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

hbrunn avatar Jan 08 '19 14:01 hbrunn

PS: Don't forget to override __str__ to print something sensible using those values

hbrunn avatar Jan 08 '19 14:01 hbrunn

Anyone working on this? If yes, please assign yourself to this issue, so it's "reserved".

m3nu avatar Jan 23 '19 09:01 m3nu

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?

bosd avatar Feb 08 '23 07:02 bosd

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.

rmilecki avatar Feb 18 '23 11:02 rmilecki