Skip to content

Commit 1cac8e0

Browse files
committed
Fixed reading server errors messages if they are multi-line
1 parent 3667f7d commit 1cac8e0

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

clickhouse-client/src/test/java/com/clickhouse/client/ClickHouseServerForTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public class ClickHouseServerForTest {
172172
ClickHouseProtocol.TCP.getDefaultSecurePort(),
173173
ClickHouseProtocol.POSTGRESQL.getDefaultPort())
174174
.withClasspathResourceMapping("containers/clickhouse-server", customDirectory, BindMode.READ_ONLY)
175+
.withClasspathResourceMapping("empty.csv", "/var/lib/clickhouse/user_files/empty.csv", BindMode.READ_ONLY)
175176
.withFileSystemBind(System.getProperty("java.io.tmpdir"), getClickHouseContainerTmpDir(),
176177
BindMode.READ_WRITE)
177178
.withNetwork(network)

clickhouse-client/src/test/resources/empty.csv

Whitespace-only changes.

client-v2/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@
6666
<artifactId>jackson-core</artifactId>
6767
<version>2.17.2</version>
6868
</dependency>
69+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
70+
<dependency>
71+
<groupId>org.apache.commons</groupId>
72+
<artifactId>commons-text</artifactId>
73+
<version>1.12.0</version>
74+
</dependency>
75+
6976
<!-- Test Dependencies -->
7077
<dependency>
7178
<groupId>com.google.code.gson</groupId>

client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.clickhouse.client.config.ClickHouseDefaults;
1717
import com.clickhouse.client.http.ClickHouseHttpProto;
1818
import com.clickhouse.client.http.config.ClickHouseHttpOption;
19+
import org.apache.commons.text.StringEscapeUtils;
1920
import org.apache.hc.client5.http.ConnectTimeoutException;
2021
import org.apache.hc.client5.http.classic.methods.HttpPost;
2122
import org.apache.hc.client5.http.config.ConnectionConfig;
@@ -63,7 +64,6 @@
6364
import java.net.NoRouteToHostException;
6465
import java.net.URI;
6566
import java.net.URISyntaxException;
66-
import java.net.URLEncoder;
6767
import java.net.UnknownHostException;
6868
import java.nio.charset.StandardCharsets;
6969
import java.security.NoSuchAlgorithmException;
@@ -303,21 +303,17 @@ public Exception readError(ClassicHttpResponse httpResponse) {
303303
}
304304
}
305305
if (found) {
306-
int start = i;
307-
while (i < rBytes && buffer[i] != '\n') {
308-
i++;
309-
}
310-
311-
return new ServerException(serverCode, new String(buffer, start, i -start, StandardCharsets.UTF_8));
306+
String msg = StringEscapeUtils.unescapeJson(new String(buffer, i, rBytes - i, StandardCharsets.UTF_8));
307+
return new ServerException(serverCode, msg.replaceAll("\n", " "));
312308
}
313309
}
314310
}
315311
}
316312
}
317313

318314
return new ServerException(serverCode, String.format(ERROR_CODE_PREFIX_PATTERN, serverCode) + " <Unreadable error message>");
319-
} catch (IOException e) {
320-
throw new ClientException("Failed to read response body", e);
315+
} catch (Exception e) {
316+
return new ServerException(serverCode, String.format(ERROR_CODE_PREFIX_PATTERN, serverCode) + " <Unreadable error message>");
321317
}
322318
}
323319

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,7 @@ public void testServerErrorHandling(ClickHouseFormat format) {
314314
.addEndpoint(server.getBaseUri())
315315
.setUsername("default")
316316
.setPassword("")
317-
.useNewImplementation(true)
318-
// TODO: fix in old client
319-
// .useNewImplementation(System.getProperty("client.tests.useNewImplementation", "false").equals("true"))
317+
.compressServerResponse(false)
320318
.build()) {
321319

322320
QuerySettings querySettings = new QuerySettings().setFormat(format);
@@ -329,6 +327,19 @@ public void testServerErrorHandling(ClickHouseFormat format) {
329327
Assert.assertTrue(e.getMessage().startsWith("Code: 62. DB::Exception: Syntax error (Multi-statements are not allowed): failed at position 15 (end of query)"),
330328
"Unexpected error message: " + e.getMessage());
331329
}
330+
331+
332+
try (QueryResponse response = client.query("CREATE TABLE table_from_csv AS SELECT * FROM file('empty.csv')", querySettings)
333+
.get(1, TimeUnit.SECONDS)) {
334+
Assert.fail("Expected exception");
335+
} catch (ServerException e) {
336+
e.printStackTrace();
337+
Assert.assertEquals(e.getCode(), 636);
338+
Assert.assertTrue(e.getMessage().startsWith("Code: 636. DB::Exception: The table structure cannot be extracted from a CSV format file. Error: The table structure cannot be extracted from a CSV format file: the file is empty. You can specify the structure manually: (in file/uri /var/lib/clickhouse/user_files/empty.csv). (CANNOT_EXTRACT_TABLE_STRUCTURE)"),
339+
"Unexpected error message: " + e.getMessage());
340+
}
341+
342+
332343
} catch (Exception e) {
333344
e.printStackTrace();
334345
Assert.fail(e.getMessage(), e);
@@ -337,7 +348,8 @@ public void testServerErrorHandling(ClickHouseFormat format) {
337348

338349
@DataProvider(name = "testServerErrorHandlingDataProvider")
339350
public static Object[] testServerErrorHandlingDataProvider() {
340-
return new Object[] { ClickHouseFormat.JSON, ClickHouseFormat.TabSeparated, ClickHouseFormat.RowBinary };
351+
return new Object[] { ClickHouseFormat.JSON, ClickHouseFormat.TabSeparated, ClickHouseFormat.RowBinary, ClickHouseFormat.TSKV,
352+
ClickHouseFormat.JSONEachRow};
341353
}
342354

343355

0 commit comments

Comments
 (0)