I get a foreign key constraint violation message when trying to delete a record in a table which has references in another table, which is absolutely normal. Now I’m trying to handle the error in an internal class and customize the message shown to the client. Which solution do you recommend (extending RuntimeException and throwing it from the middleware to the client?) The documentation is not very clear. Other threads discuss only cuba.uniqueConstraintViolationPattern which is not useful in this case.
A second question: Working with a screen extending AbstractLookup, I want to override the method which saves the instance in that screen. After the user creates, edits or removes a record, a Confirmation dialog opens: “Are you sure you want to delete selected elements?”. I want to override the ok action of that dialog (assuming that it is the right place where the lookup-screen is commited). Also it has something to do with the first question (this way I can include the commit method in a try-catch block and customize my message)
The recommended way to handle exceptions is to register a Client-Level Exception Handler. It must be a managed bean of the client tier extending the AbstractGenericExceptionHandler class. In the constructor, define class names of exceptions to handle. For example:
Yes, the exception class may depend on the database type in use. You could also list all possible exception types in the constructor, and even additionally implement the canHandle() method for more fine-grained control of what exceptions to handle.
Another question regarding exceptions.
Say I have an SQL exception (for ex. foreign key violation like in the ex. above) on a screen commit. Is there a way to pass parameters to the exception class located in the gui module (ForeignKeyViolationExceptionHandler) from the web module?
I want a more specific error message which includes deleted row values: the message should look something like this: “You cannot delete the product: “+ sProductName +”. It has references in other objects!” .
In this example project: GitHub - cuba-platform/sample-data-manipulation: DEPRECATED. See https://www.cuba-platform.com/guides/intro-working-with-data-in-cuba
; errors with parameters are thrown from the middleware. I tried to use classic try - catch blocks, encapsulating screen commit, but it won’t catch exceptions form the middleware.
You cannot pass any parameters to exception handlers, they can use only the information from the exception itself. However catching exceptions on screen commit should work. Could you provide the code of your try-catch block?
At runtime, without try-catch, the super.actionPerform(component); command generates the foreign key violation error (first the deletion confirmation dialog is shown, assuming the user clicks OK).
With try-catch, as shown above, the program seems to continue execution after super.actionPerform(component); and both the deletion confirmation message and the Test message are shown simultaneously. So My custom error message is never reached.
It behaves as if the actionperform method will execute in a different thread.
You override the wrong method: actionPerform() just invokes the confirmation dialog and returns, the actual removing is done later in a callback method invoked by the OK action of the dialog. So you should override this callback method:
This post demonstrates a way to override the removeaction, that is not working properly anymore with Cuba 7, as there seem to be several interface changes.
What would be the correct new way, to override the action? The actual problem is, that I cannot instantiate a valid RemoveAction base object with a valid, non-null (injected) removeOperation field, resulting in a NullPointerException.