Skip to content

Commit 5145aa5

Browse files
javier-godoypaodb
authored andcommitted
feat: add error view
1 parent b739b9e commit 5145aa5

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.flowingcode.vaadin.addons.errorwindow;
2+
3+
import com.vaadin.flow.component.UI;
4+
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
5+
import com.vaadin.flow.router.BeforeEnterEvent;
6+
import com.vaadin.flow.router.ErrorParameter;
7+
import com.vaadin.flow.router.HasErrorParameter;
8+
import com.vaadin.flow.router.internal.DefaultErrorHandler;
9+
import org.apache.http.HttpStatus;
10+
11+
@SuppressWarnings("serial")
12+
@DefaultErrorHandler
13+
public class ErrorView extends VerticalLayout implements HasErrorParameter<Exception> {
14+
15+
@Override
16+
public int setErrorParameter(BeforeEnterEvent event, ErrorParameter<Exception> parameter) {
17+
setSizeFull();
18+
19+
getElement().getThemeList().add("error-window");
20+
21+
ErrorWindowI18n i18n = ErrorWindowI18n.createDefault();
22+
i18n.setClose("Back");
23+
new ErrorWindow(parameter.getCaughtException(), i18n) {
24+
@Override
25+
public void close() {
26+
UI.getCurrent().getPage().getHistory().back();
27+
}
28+
}.getChildren().forEach(this::add);
29+
30+
return HttpStatus.SC_INTERNAL_SERVER_ERROR;
31+
}
32+
33+
}

src/main/java/com/flowingcode/vaadin/addons/errorwindow/VaadinServiceInitListenerImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
package com.flowingcode.vaadin.addons.errorwindow;
2222

2323
import com.vaadin.flow.component.UI;
24+
import com.vaadin.flow.router.InternalServerError;
2425
import com.vaadin.flow.server.ErrorEvent;
2526
import com.vaadin.flow.server.ServiceInitEvent;
27+
import com.vaadin.flow.server.VaadinContext;
28+
import com.vaadin.flow.server.VaadinService;
2629
import com.vaadin.flow.server.VaadinServiceInitListener;
30+
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
31+
import java.util.Collections;
2732

2833
public class VaadinServiceInitListenerImpl implements VaadinServiceInitListener {
2934

@@ -34,6 +39,17 @@ public void serviceInit(ServiceInitEvent event) {
3439
event
3540
.getSource()
3641
.addSessionInitListener(ev -> ev.getSession().setErrorHandler(this::handleError));
42+
registerErrorView(event.getSource());
43+
}
44+
45+
private void registerErrorView(VaadinService source) {
46+
VaadinContext context = VaadinService.getCurrent().getContext();
47+
ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(context);
48+
Class<?> configuredExceptionHandler =
49+
registry.getConfiguration().getExceptionHandlerByClass(Exception.class);
50+
if (configuredExceptionHandler == InternalServerError.class) {
51+
registry.setErrorNavigationTargets(Collections.singleton(ErrorView.class));
52+
}
3753
}
3854

3955
private void handleError(ErrorEvent event) {

src/test/java/com/flowingcode/vaadin/addons/errorwindow/ErrorwindowDemo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package com.flowingcode.vaadin.addons.errorwindow;
2222

23+
import com.vaadin.flow.component.UI;
2324
import com.vaadin.flow.component.button.Button;
2425
import com.vaadin.flow.component.checkbox.Checkbox;
2526
import com.vaadin.flow.component.notification.Notification;
@@ -93,5 +94,8 @@ public ErrorwindowDemo() {
9394
throwErrorWithoutErrorHandler,
9495
throwErrorWithCustomMessageAndCustomTexts,
9596
throwErrorWithCustomMessage);
97+
98+
add(new Button("Navigation error",
99+
ev -> UI.getCurrent().navigate(ThrowInConstructorView.class)));
96100
}
97101
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.flowingcode.vaadin.addons.errorwindow;
2+
3+
import com.vaadin.flow.component.html.Div;
4+
import com.vaadin.flow.router.Route;
5+
6+
@Route("error-window/throw")
7+
public class ThrowInConstructorView extends Div {
8+
9+
public ThrowInConstructorView() {
10+
throw new RuntimeException();
11+
}
12+
13+
}

0 commit comments

Comments
 (0)