Programming tips: constraints and exceptions
OpenERP provides a standard mechanism for adding constraints to records. This is used, for example, in account.move.line to ensure that no account of type view is used in an account move line. At first glance, this functionality seems useful, but experience shows that the current constraints mechanism has serious usability problems.
The reason is quite simple. If we consider the example of account.move.line, a user may fill in a large bank statement and then try to confirm it once it is complete. This is when the account moves are created, and if just one of them uses an account of type view, the user will always receive the same message: “You cannot create a move line on a view account.” Perfect. But which of the 100 lines in the statement is the incorrect one?
Something similar happens when developers fail to consider this issue when raising exceptions. For example, if one of the 100 lines in the bank statement has already been reconciled, the user will receive the highly informative message: Already Reconciled.
These are very important considerations in production environments, and we have had to make several changes to improve exceptions, especially in accounting modules. A simple message such as “Account move line %s, with debit %.2f and credit %.2f, has already been reconciled” does the job and can save OpenERP users a great deal of trouble.
It is important to bear in mind that, if the record has just been created, the user may be unable to locate the problematic record because rolling back the transaction may remove it. It is therefore always advisable to provide as much information as possible.
Finally, because standard constraint messages do not allow all the necessary information to be included, the best approach is to use the constraints mechanism while raising an informative exception from the constraint function, instead of allowing OpenERP to return the standard constraint message to the user.