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

4.x: ErrorHandlers class swallows exception object if response can't be reset #8634

Merged
merged 2 commits into from
Apr 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private void handleError(ConnectionContext ctx,
if (!response.reset()) {
ctx.log(LOGGER, System.Logger.Level.WARNING, "Unable to reset response for error handler.");
throw new CloseConnectionException(
"Cannot send response of a simple handler, status and headers already written");
"Cannot send response of a simple handler, status and headers already written", e);
}
try {
it.handle(request, response, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
import io.helidon.http.Method;
import io.helidon.http.Status;
import io.helidon.http.media.ReadableEntityBase;
import io.helidon.webserver.CloseConnectionException;
import io.helidon.webserver.ConnectionContext;
import io.helidon.webserver.ListenerContext;

Expand All @@ -43,6 +44,8 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -121,6 +124,24 @@ void testHandler() {
testNoHandler(handlers, new OtherException(), "Other");
}

@Test
public void testCloseConnectionExceptionContainsCause() {
ConnectionContext ctx = mock(ConnectionContext.class);
RoutingRequest req = mock(RoutingRequest.class);
RoutingResponse res = mock(RoutingResponse.class);
when(res.reset()).thenReturn(false);
ErrorHandlers handlers = ErrorHandlers.create(Map.of(OtherException.class,
(request, response, t) -> res.send(t.getMessage())));
try {
handlers.runWithErrorHandling(ctx, req, res, () -> {
throw new OtherException();
});
fail("It is expected a CloseConnectionException");
} catch (CloseConnectionException e) {
assertEquals(OtherException.class, e.getCause().getClass());
}
}

private void testNoHandler(ErrorHandlers handlers, Exception e, String message) {
ConnectionContext ctx = mock(ConnectionContext.class);
RoutingRequest req = mock(RoutingRequest.class);
Expand Down