File tree Expand file tree Collapse file tree 4 files changed +66
-0
lines changed
main/java/com/flowingcode/vaadin/addons/errorwindow
test/java/com/flowingcode/vaadin/addons/errorwindow Expand file tree Collapse file tree 4 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 2121package com .flowingcode .vaadin .addons .errorwindow ;
2222
2323import com .vaadin .flow .component .UI ;
24+ import com .vaadin .flow .router .InternalServerError ;
2425import com .vaadin .flow .server .ErrorEvent ;
2526import com .vaadin .flow .server .ServiceInitEvent ;
27+ import com .vaadin .flow .server .VaadinContext ;
28+ import com .vaadin .flow .server .VaadinService ;
2629import com .vaadin .flow .server .VaadinServiceInitListener ;
30+ import com .vaadin .flow .server .startup .ApplicationRouteRegistry ;
31+ import java .util .Collections ;
2732
2833public 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 ) {
Original file line number Diff line number Diff line change 2020
2121package com .flowingcode .vaadin .addons .errorwindow ;
2222
23+ import com .vaadin .flow .component .UI ;
2324import com .vaadin .flow .component .button .Button ;
2425import com .vaadin .flow .component .checkbox .Checkbox ;
2526import 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments