How to communicate a critical error to the user?

Hello! I’m using Vaadin 24.7.

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.

Are you sure the dialog is really throwing the exception within the constructor?

This pseudo code should not open a dialog if MyDialog throws an exception. If it happens I would say it’s s bug in the Framework

try
new MyDialog().open();
catch (E e)
Notification.show(e.getMessage());

The exception that is thrown by the backend in a method called within the Dialog constructor is caught.
Pseudo code:

var dialog = new XDialog();
dialog.open();

public class XDialog {

  public xDialog() {
    aMethod();
  }

  private aMethod() {
    try {
      callBackend();
    } catch (SQLException exception) {
      logger.error("An error occurred during loading the record", exception);
      showNotification();
    }
  }
}

Yeah the problem is your open() call which does not know if it should run or not… open should only be called if your backend call is successful.

There are multiple refactoring possibilities to archive that.

To your original question: I would say the critical notification is more like an internal thing - not to be used by Developer directly

Thank you for the quick answer.

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.

What do you think?

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