Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Do not throw NPE for UI.access called when session has expired (#12531) (#12591) (CP: 2.7) #12596

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions flow-server/src/main/java/com/vaadin/flow/component/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -515,9 +516,24 @@ public void handleError(Exception exception) {
if (command instanceof ErrorHandlingCommand) {
ErrorHandlingCommand errorHandlingCommand = (ErrorHandlingCommand) command;
errorHandlingCommand.handleError(exception);
} else {
} else if (getSession() != null) {
getSession().getErrorHandler()
.error(new ErrorEvent(exception));
} else {
/*
* The session has expired after `ui.access` was called.
* It makes no sense to pollute the logs with a
* UIDetachedException at this point.
*/
if (exception instanceof ExecutionException
&& ((ExecutionException) exception)
.getCause() instanceof UIDetachedException) {
getLogger().debug(exception.getMessage(),
exception);
} else {
getLogger().error(exception.getMessage(),
exception);
}
}
} catch (Exception e) {
getLogger().error(e.getMessage(), e);
Expand Down Expand Up @@ -696,7 +712,7 @@ public ReconnectDialogConfiguration getReconnectDialogConfiguration() {
return getNode().getFeature(ReconnectDialogConfigurationMap.class);
}

private static Logger getLogger() {
Logger getLogger() {
return LoggerFactory.getLogger(UI.class.getName());
}

Expand Down
37 changes: 36 additions & 1 deletion flow-server/src/test/java/com/vaadin/flow/component/UITest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.vaadin.flow.component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -15,6 +16,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.slf4j.Logger;

import com.vaadin.flow.component.internal.PendingJavaScriptInvocation;
import com.vaadin.flow.component.page.History;
Expand Down Expand Up @@ -61,6 +63,7 @@
import com.vaadin.flow.server.VaadinResponse;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinServletRequest;
import com.vaadin.flow.server.frontend.MockLogger;
import com.vaadin.tests.util.AlwaysLockedVaadinSession;
import com.vaadin.tests.util.MockUI;

Expand Down Expand Up @@ -163,11 +166,17 @@ public void elementIsBody() {
}

private static UI createTestUI() {
MockLogger mockLogger = new MockLogger();
UI ui = new UI() {
@Override
public void doInit(VaadinRequest request, int uiId) {

}

@Override
Logger getLogger() {
return mockLogger;
}
};

return ui;
Expand Down Expand Up @@ -439,7 +448,7 @@ public void unsetSession_detachEventIsFiredForUIChildren()
}

@Test
public void unserSession_datachEventIsFiredForElements() {
public void unsetSession_detachEventIsFiredForElements() {
UI ui = createTestUI();

List<ElementDetachEvent> events = new ArrayList<>();
Expand All @@ -461,6 +470,32 @@ public void unserSession_datachEventIsFiredForElements() {
assertEquals(ui.getElement(), events.get(1).getSource());
}

@Test
public void unsetSession_accessErrorHandlerStillWorks() throws IOException {
UI ui = createTestUI();
initUI(ui, "", null);

ui.getSession().access(() -> ui.getInternals().setSession(null));
ui.access(() -> {
Assert.fail("We should never get here because the UI is detached");
});

// Unlock to run pending access tasks
ui.getSession().unlock();

String logOutput = ((MockLogger) ui.getLogger()).getLogs();
String logOutputNoDebug = logOutput.replaceAll("^\\[Debug\\].*", "");

Assert.assertFalse(
"No NullPointerException should be logged but got: "
+ logOutput,
logOutput.contains("NullPointerException"));
Assert.assertFalse(
"No UIDetachedException should be logged but got: "
+ logOutputNoDebug,
logOutputNoDebug.contains("UIDetachedException"));
}

@Test
public void beforeClientResponse_regularOrder() {
UI ui = createTestUI();
Expand Down