I’m trying to setup an ExceptionHandler for the NullpointerException, following the [documentation]
(https://vaadin.com/docs/v14/flow/routing/tutorial-routing-exception-handling.html).
I have already implemented ExceptionHandlers for other Exceptions (my own exceptions) and they work great, but I’m having trouble with handling NPEs.
Basically, when a NPE occurs, I see the stacktrace in my console, but in the browser nothing happens. Is this by design? Is maybe the NPE an exception in the exception handling logic of vaadin?
Funny is though, that when I call beforeEvent.rerouteToError(NullPointerException.class)
, then it works. But only by calling rerouteToError(…). Here is an example View that shows this.
@Route(value = "Test")
public class TestView extends VerticalLayout implements HasUrlParameter<String> {
private static final Logger LOGGER = LogManager.getLogger(TestView.class);
public TestView() {
add(new Button("Cause NPE!", click -> {
Object obj = null;
obj.toString();
}));
add(new Button("Reroute to NPE handler", click -> {
UI.getCurrent().navigate(TestView.class, "gogogo");
}));
}
@Override
public void setParameter(BeforeEvent beforeEvent, @OptionalParameter String s) {
if(s != null && s.equals("gogogo")){
beforeEvent.rerouteToError(NullPointerException.class);
}
}
}
public class NPEHandler extends VerticalLayout implements HasErrorParameter<NullPointerException> {
@Override
public int setErrorParameter(BeforeEnterEvent event, ErrorParameter<NullPointerException> parameter) {
// general message about NPE
add(new Label(getTranslation(NULLPOINTER_EXCEPTION_MSG.getKey())));
// actual exception message
add(new Label(parameter.getCaughtException().getMessage()));
// exception cause
StackTraceElement[] stackTrace = parameter.getException().getStackTrace();
VerticalLayout stackTraceLayout = new VerticalLayout();
for (StackTraceElement stackTraceElement : stackTrace) {
stackTraceLayout.add(new Label(stackTraceElement.toString()));
}
add(stackTraceLayout);
return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}
}
Please advise if this is intentional, or how I can make it work. Thank you