Skip to content

Commit

Permalink
4.x: ErrorHandlers class swallows exception object if response can't …
Browse files Browse the repository at this point in the history
…be reset (#8634)

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

Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos authored Apr 11, 2024
1 parent aeff591 commit df16682
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
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

0 comments on commit df16682

Please sign in to comment.