Skip to content

Commit

Permalink
fix Error Handler not displaying Notification, make Views use ErrorHa…
Browse files Browse the repository at this point in the history
…ndler

Signed-off-by: Aleksandar Zivkovic <[email protected]>
  • Loading branch information
anywaywayany committed Feb 26, 2024
1 parent c25203c commit e31a675
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 34 deletions.
25 changes: 6 additions & 19 deletions src/main/java/com/samic/samic/views/SamicErrorHandler.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
package com.samic.samic.views;

import com.samic.samic.components.UIFactory;
import com.samic.samic.exceptions.SamicException;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.ErrorParameter;
import com.vaadin.flow.router.HasErrorParameter;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.router.ParentLayout;
import com.vaadin.flow.server.ErrorEvent;
import com.vaadin.flow.server.ErrorHandler;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@PermitAll
@ParentLayout(MainLayout.class)
public class SamicErrorHandler extends VerticalLayout implements ErrorHandler,
HasErrorParameter<SamicException> {
public class SamicErrorHandler implements ErrorHandler {

private static final Logger logger = LoggerFactory.getLogger(SamicErrorHandler.class);

@Override
public void error(ErrorEvent errorEvent) {
logger.error("Something wrong happened", errorEvent.getThrowable());
if (UI.getCurrent() != null) {
UI.getCurrent().access(() -> UIFactory.notificationError("An internal error has occurred." +
"Contact support for assistance."));
Notification notification = UIFactory.notificationError(
errorEvent.getThrowable().getMessage());
UI.getCurrent().add(notification);
notification.open();
}
}

@Override
public int setErrorParameter(BeforeEnterEvent event,
ErrorParameter<SamicException>
parameter) {
getElement().setText(parameter.getCaughtException().getMessage());
logger.info("Error: {}", parameter.getException().getMessage());
return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}
}
17 changes: 2 additions & 15 deletions src/main/java/com/samic/samic/views/SamicServiceListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,9 @@ public class SamicServiceListener implements VaadinServiceInitListener {

@Override
public void serviceInit(ServiceInitEvent event) {
event.addIndexHtmlRequestListener(response -> {
// IndexHtmlRequestListener to change the bootstrap page
});
event.getSource().addSessionInitListener(
initEvent -> VaadinSession.getCurrent().setErrorHandler(new SamicErrorHandler()));

event.addDependencyFilter((dependencies, filterContext) -> {
// DependencyFilter to add/remove/change dependencies sent to
// the client
return dependencies;
});

event.addRequestHandler((session, request, response) -> {
// RequestHandler to change how responses are handled
return false;
});

VaadinSession.getCurrent().setErrorHandler(new SamicErrorHandler());
}

}

0 comments on commit e31a675

Please sign in to comment.