Custom Error Handling
You can customize the error views related to navigation between different routes as described in the Router Exception Handling section.
For other unhandled runtime exceptions, an ErrorHandler
class can be used to gracefully let users know that something went wrong. In order to do so,
first create your custom ErrorHandler
class, then use that class to override the default error handler via VaadinSession.getCurrent().setErrorHandler(ErrorHandler)
.
For example, the following CustomErrorHandler
class logs the error and displays a friendly notification to the user.
public class CustomErrorHandler implements ErrorHandler {
private static final Logger logger = LoggerFactory.getLogger(CustomErrorHandler.class);
@Override
public void error(ErrorEvent errorEvent) {
logger.error("Something wrong happened", errorEvent.getThrowable());
if(UI.getCurrent() != null) {
UI.getCurrent().access(() -> {
Notification.show("An internal error has occurred." +
"Please contact support.");
});
}
}
}
In order for the logic in this class to take effect, you can then assign it to the current user session using VaadinSession.getCurrent().setErrorHandler(new CustomErrorHandler());
. In order to apply the custom error-handling logic to all user sessions, it is usually more convenient to make use of a SessionInitListener
, which receives an event each time a new VaadinSession
is initialized (see Session and UI Listeners tutorial to learn how to create a SessionInitListener
).
69A9E596-86A9-4559-BB1F-216BD8C1AB37