From a2b045a45e3c42bc070650842e29bcfe084688e6 Mon Sep 17 00:00:00 2001 From: Christoph Pirkl Date: Wed, 18 Oct 2023 14:42:35 +0200 Subject: [PATCH] Improve error messages --- error_code_config.yml | 2 +- .../exasol/containers/ExasolContainer.java | 10 +++---- .../containers/ExasolContainerTest.java | 27 +++++++++++++------ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/error_code_config.yml b/error_code_config.yml index 5c9e9a1d..fb25dfcf 100644 --- a/error_code_config.yml +++ b/error_code_config.yml @@ -5,4 +5,4 @@ error-tags: - com.exasol.support - com.exasol.clusterlogs - com.exasol.bucketfs.testcontainers - highest-index: 23 \ No newline at end of file + highest-index: 24 diff --git a/src/main/java/com/exasol/containers/ExasolContainer.java b/src/main/java/com/exasol/containers/ExasolContainer.java index f6f46475..143aa1e1 100644 --- a/src/main/java/com/exasol/containers/ExasolContainer.java +++ b/src/main/java/com/exasol/containers/ExasolContainer.java @@ -715,7 +715,7 @@ private void waitUntilStatementCanBeExecuted() { + " after {{after}} seconds. Last connection exception was: {{exception}}") .parameter("url", getJdbcUrl(), "JDBC URL of the connection to the Exasol Testcontainer") .parameter("query", getTestQueryString(), "Query used to test the connection") - .parameter("after", timeoutAfter.toSeconds() + "." + timeoutAfter.toSecondsPart()) + .parameter("after", timeoutAfter.toSeconds()) .parameter("exception", (this.lastConnectionException == null) ? "none" : this.lastConnectionException.getMessage(), "exception thrown on last connection attempt") @@ -741,9 +741,9 @@ private boolean isConnectionAvailable() { throw new ContainerLaunchException("Startup check query failed. Exasol container start-up failed."); } } catch (final NoDriverFoundException exception) { - throw new ContainerLaunchException( - "Unable to determine start status of container, because the referenced JDBC driver was not found.", - exception); + throw new ContainerLaunchException(ExaError.messageBuilder("E-ETC-24").message( + "Unable to determine start status of container, because the referenced JDBC driver was not found: {{cause}}", + exception.getMessage()).toString(), exception); } catch (final SQLException exception) { this.lastConnectionException = exception; sleepBeforeNextConnectionAttempt(); @@ -1067,4 +1067,4 @@ public ExecResult probeFile(final String path) { throw new SshException("Probing file " + path + "was interrupted", interruptedException); } } -} \ No newline at end of file +} diff --git a/src/test/java/com/exasol/containers/ExasolContainerTest.java b/src/test/java/com/exasol/containers/ExasolContainerTest.java index 71e4ffc4..1360935d 100644 --- a/src/test/java/com/exasol/containers/ExasolContainerTest.java +++ b/src/test/java/com/exasol/containers/ExasolContainerTest.java @@ -14,6 +14,8 @@ import java.sql.SQLException; import java.time.Duration; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; import org.junit.jupiter.api.*; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.function.Executable; @@ -36,8 +38,9 @@ class ExasolContainerTest { @BeforeEach void beforeEach() throws NoDriverFoundException { - final ExasolContainer container = new ExasolContainer<>(); + final ExasolContainer> container = new ExasolContainer<>(); container.withRequiredServices(); + container.withJdbcConnectionTimeout(10); this.containerSpy = spy(container); } @@ -48,20 +51,28 @@ void testWaitUntilContainerStartedTimesOut() throws Exception { doNothing().when(this.containerSpy).waitUntilClusterConfigurationAvailable(); doReturn(this.connectionMock).when(this.containerSpy).createConnection(any()); when(this.connectionMock.createStatement()).thenThrow(new SQLException("Mock Exception")); - assertThrowsLaunchException("timed out", () -> this.containerSpy.waitUntilContainerStarted()); + assertThrowsLaunchException( + allOf(Matchers + .startsWith("F-ETC-5: Exasol container start-up timed out trying connection to 'jdbc:exa:"), + Matchers.endsWith("Last connection exception was: 'Mock Exception'")), + () -> this.containerSpy.waitUntilContainerStarted()); } - private void assertThrowsLaunchException(final String expectedMessageFragment, final Executable executable) { + private void assertThrowsLaunchException(final Matcher expectedMessage, final Executable executable) { final ContainerLaunchException exception = assertThrows(ContainerLaunchException.class, executable); - assertThat(exception.getMessage(), containsString(expectedMessageFragment)); + assertThat(exception.getMessage(), expectedMessage); } @Test void testWaitUntilContainerStartedThrowsExceptionOnMissingJdbcDriver() throws NoDriverFoundException, SQLException { doNothing().when(this.containerSpy).waitUntilClusterConfigurationAvailable(); - Mockito.doThrow(new NoDriverFoundException("Mock Driver-Not-Found Exception", new Exception("Mock cause"))) - .when(this.containerSpy).createConnection(anyString()); - assertThrowsLaunchException("driver was not found", () -> this.containerSpy.waitUntilContainerStarted()); + final String message = "Mock Driver-Not-Found Exception"; + Mockito.doThrow(new NoDriverFoundException(message, new Exception("Mock cause"))).when(this.containerSpy) + .createConnection(anyString()); + assertThrowsLaunchException(equalTo( + "E-ETC-24: Unable to determine start status of container, because the referenced JDBC driver was not found: '" + + message + "'"), + () -> this.containerSpy.waitUntilContainerStarted()); } @SuppressWarnings("deprecation") @@ -182,4 +193,4 @@ void sessionBuilder() { assertThat(session.getPort(), equalTo(321)); assertThat(session.getUserName(), equalTo(SSH_USER)); } -} \ No newline at end of file +}