0

I wrote my own custom error handler for the UI in Vaadin flow. But when I throw the exception my view crash and not show my human readable error message.

I did this in other application using Vaadin 8 and works perfectly. The idea its throw a SgiException in my backend services like:

  • Product not found
  • Incorrect value for field "XXX"
  • Not available stock for the product.
  • etc.

And then show a system notification

        public static void setDefaultErrorHandler(ErrorEvent errorEvent) {

            Throwable t = DefaultErrorHandler.findRelevantThrowable(errorEvent.getThrowable());

            String message;
            if (t != null) {
                message = t.getMessage();
            } else {
                message = "";
            }

            log.error(message, t);

            SgiException sgiException = getCauseOfType(t, SgiException.class);
            if (sgiException != null) {
                NotificationBuilder.exception(sgiException.getCode(), sgiException.getMessage());
                return;
            } else {
                NotificationBuilder.exception(UNKNOW_ERROR, (message == null ? "" : message));
                return;
            }
        }

        private static <T extends Throwable> T getCauseOfType(Throwable th, Class<T> type) {
            while (th != null) {
                if (type.isAssignableFrom(th.getClass())) {
                    return (T) th;
                } else {
                    th = th.getCause();
                }
            }
            return null;
        }

And this is how I set the custom error handler:

        @PostConstruct
        public void configBaseView() {
            VaadinSession.getCurrent().setErrorHandler(Util::setDefaultErrorHandler);
        }

In the view show this:

enter image description here

Note: Debugging the application, seeing the code it's running, looks the method its called for some reason not show the notification.

Carlos Laspina
  • 2,013
  • 4
  • 27
  • 44

1 Answers1

2

This is a nasty behaviour that can't currently be overridden in Vaadin 10. Follow and vote (thumb up or comment) this issue to get it solved: https://github.com/vaadin/flow/issues/801

mstahv
  • 1,784
  • 9
  • 7