Skip to content

Commit 9e5367e

Browse files
authored
Merge pull request #2869 from ClickHouse/06/09/26/verify_error_handling_issue
Added verification test for error in OK response
2 parents 6f083fc + 90ce7dd commit 9e5367e

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

client-v2/src/test/java/com/clickhouse/client/HttpTransportTests.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
1515
import com.clickhouse.client.api.enums.Protocol;
1616
import com.clickhouse.client.api.enums.ProxyType;
17+
import com.clickhouse.client.api.http.ClickHouseHttpProto;
1718
import com.clickhouse.client.api.insert.InsertResponse;
1819
import com.clickhouse.client.api.insert.InsertSettings;
1920
import com.clickhouse.client.api.internal.DataTypeConverter;
@@ -2084,6 +2085,55 @@ public void testConcurrentTokenChange() throws Exception {
20842085
}
20852086
}
20862087

2088+
@Test(groups = {"integration"}, dataProvider = "testErrorWhenResponseIsOk_Dp")
2089+
public void testErrorWhenResponseIsOk(boolean hasMsg) throws Exception {
2090+
WireMockServer mockServer = new WireMockServer(WireMockConfiguration
2091+
.options().port(9090).notifier(new ConsoleNotifier(false)));
2092+
mockServer.start();
2093+
2094+
try (Client client = new Client.Builder().addEndpoint(Protocol.HTTP, "localhost", mockServer.port(), false)
2095+
.setUsername("default")
2096+
.setPassword(ClickHouseServerForTest.getPassword())
2097+
.build()) {
2098+
2099+
final String errorMsg = hasMsg ? "Code: 60. DB::Exception: Unknown table expression identifier" : "";
2100+
mockServer.addStubMapping(WireMock.post(WireMock.anyUrl())
2101+
.willReturn(WireMock.aResponse()
2102+
.withStatus(HttpStatus.SC_OK)
2103+
.withHeader(ClickHouseHttpProto.HEADER_EXCEPTION_CODE, "60")
2104+
.withBody(errorMsg))
2105+
.build());
2106+
2107+
2108+
try (QueryResponse response = client.query("SELECT * FROM not_existing_table").get(1, TimeUnit.SECONDS)) {
2109+
Assert.fail("Expected exception");
2110+
} catch (Exception e) {
2111+
ServerException se = null;
2112+
if (e instanceof ServerException) {
2113+
se = (ServerException) e;
2114+
} else if (e.getCause() instanceof ServerException) {
2115+
se = (ServerException) e.getCause();
2116+
} else {
2117+
Assert.fail("Unexpected exception type", e);
2118+
}
2119+
2120+
Assert.assertEquals(se.getCode(), 60);
2121+
if (hasMsg) {
2122+
Assert.assertEquals(se.getMessage().substring(0, errorMsg.length()), errorMsg);
2123+
} else {
2124+
Assert.assertTrue(se.getMessage().contains("<Unreadable error message>"), "Error message was " + se.getMessage());
2125+
}
2126+
}
2127+
} finally {
2128+
mockServer.stop();
2129+
}
2130+
}
2131+
2132+
@DataProvider
2133+
public static Object[][] testErrorWhenResponseIsOk_Dp() {
2134+
return new Object[][]{{true}, {false}};
2135+
}
2136+
20872137
protected Client.Builder newClient() {
20882138
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
20892139
boolean isSecure = isCloud();

0 commit comments

Comments
 (0)