Skip to content

Commit 935cec8

Browse files
committed
fix reading error
1 parent b0851f8 commit 935cec8

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ private boolean readFully(byte[] b, int off, int len) throws IOException {
9191
return true;
9292
}
9393

94+
public byte[] getHeaderBuffer() {
95+
return headerBuff;
96+
}
97+
98+
public InputStream getInputStream() {
99+
return in;
100+
}
101+
94102
private int refill() throws IOException {
95103

96104
// read header

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ public CloseableHttpClient createHttpClient(boolean initSslContext, Map<String,
332332
return clientBuilder.build();
333333
}
334334

335-
private static final String ERROR_CODE_PREFIX_PATTERN = "Code: %d. DB::Exception:";
335+
// private static final String ERROR_CODE_PREFIX_PATTERN = "Code: %d. DB::Exception:";
336+
private static final String ERROR_CODE_PREFIX_PATTERN = "%d. DB::Exception:";
336337

337338
/**
338339
* Reads status line and if error tries to parse response body to get server error message.
@@ -342,14 +343,27 @@ public CloseableHttpClient createHttpClient(boolean initSslContext, Map<String,
342343
*/
343344
public Exception readError(ClassicHttpResponse httpResponse) {
344345
int serverCode = getHeaderInt(httpResponse.getFirstHeader(ClickHouseHttpProto.HEADER_EXCEPTION_CODE), 0);
345-
try (InputStream body = httpResponse.getEntity().getContent()) {
346-
346+
InputStream body = null;
347+
try {
348+
body = httpResponse.getEntity().getContent();
347349
byte[] buffer = new byte[ERROR_BODY_BUFFER_SIZE];
348350
byte[] lookUpStr = String.format(ERROR_CODE_PREFIX_PATTERN, serverCode).getBytes(StandardCharsets.UTF_8);
349351
StringBuilder msgBuilder = new StringBuilder();
350352
boolean found = false;
351353
while (true) {
352-
int rBytes = body.read(buffer);
354+
int rBytes = -1;
355+
try {
356+
rBytes = body.read(buffer);
357+
} catch (ClientException e) {
358+
// Invalid LZ4 Magic
359+
if (body instanceof ClickHouseLZ4InputStream) {
360+
ClickHouseLZ4InputStream stream = (ClickHouseLZ4InputStream) body;
361+
body = stream.getInputStream();
362+
byte[] headerBuffer = stream.getHeaderBuffer();
363+
System.arraycopy(headerBuffer, 0, buffer, 0, headerBuffer.length);
364+
rBytes = headerBuffer.length;
365+
}
366+
}
353367
if (rBytes == -1) {
354368
break;
355369
}
@@ -388,7 +402,7 @@ public Exception readError(ClassicHttpResponse httpResponse) {
388402
if (msg.trim().isEmpty()) {
389403
msg = String.format(ERROR_CODE_PREFIX_PATTERN, serverCode) + " <Unreadable error message> (transport error: " + httpResponse.getCode() + ")";
390404
}
391-
return new ServerException(serverCode, msg, httpResponse.getCode());
405+
return new ServerException(serverCode, "Code: " + msg, httpResponse.getCode());
392406
} catch (Exception e) {
393407
LOG.error("Failed to read error message", e);
394408
return new ServerException(serverCode, String.format(ERROR_CODE_PREFIX_PATTERN, serverCode) + " <Unreadable error message> (transport error: " + httpResponse.getCode() + ")", httpResponse.getCode());

0 commit comments

Comments
 (0)