I’d like to notify the user if there was an exception during processing his/her request. It could be an exception thrown during loading a record from the database for example.
My first iteration was to show a Notification whenever an exception is thrown. This works, but I ran into the following scenario:
I have a Grid, the user clicks on an item
When clicking on an item, a Dialog is created
In a method called from the Dialog constructor an exception is thrown
The exception is caught in that method and a Notification is shown
Execution of the code is continuing
The Dialog is then opened from the view that contains the Grid the user clicked on
The end result is:
the Notification is shown
the Dialog is opened
you can’t close the Notification as it is inert
if you close the Dialog you can close the Notification
My question is: is it a recommended approach to use the com.vaadin.flow.server.VaadinService#createCriticalNotificationJSON() to communicate this type of critical errors to the user?
I tested it, if I use this method to communicate the critical error to the user via this method at the spot where I catch the exception, then the UI works as expected. The Dialog is not shown, the request processing is stopped and the critical error is shown to the user.
Okay, I think then I’ll refactor the common notification handler component (showNotification() in my example) to throw a special exception that will be caught by a registered custom error handler, instead of showing a Notification.
Depends on your application flow / design and user requirement. For example some might argue that you should open the dialog, show the dedicated error within the dialog with a retry button to allow people to retry.
If you already have a globally registered Errorhandler, I would personally not even show a notification here but either just throw or re-throw the exception and let it the error handler do its job