@@ -332,7 +332,8 @@ public CloseableHttpClient createHttpClient(boolean initSslContext, Map<String,
332
332
return clientBuilder .build ();
333
333
}
334
334
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:" ;
336
337
337
338
/**
338
339
* 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,
342
343
*/
343
344
public Exception readError (ClassicHttpResponse httpResponse ) {
344
345
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 ();
347
349
byte [] buffer = new byte [ERROR_BODY_BUFFER_SIZE ];
348
350
byte [] lookUpStr = String .format (ERROR_CODE_PREFIX_PATTERN , serverCode ).getBytes (StandardCharsets .UTF_8 );
349
351
StringBuilder msgBuilder = new StringBuilder ();
350
352
boolean found = false ;
351
353
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
+ }
353
367
if (rBytes == -1 ) {
354
368
break ;
355
369
}
@@ -388,7 +402,7 @@ public Exception readError(ClassicHttpResponse httpResponse) {
388
402
if (msg .trim ().isEmpty ()) {
389
403
msg = String .format (ERROR_CODE_PREFIX_PATTERN , serverCode ) + " <Unreadable error message> (transport error: " + httpResponse .getCode () + ")" ;
390
404
}
391
- return new ServerException (serverCode , msg , httpResponse .getCode ());
405
+ return new ServerException (serverCode , "Code: " + msg , httpResponse .getCode ());
392
406
} catch (Exception e ) {
393
407
LOG .error ("Failed to read error message" , e );
394
408
return new ServerException (serverCode , String .format (ERROR_CODE_PREFIX_PATTERN , serverCode ) + " <Unreadable error message> (transport error: " + httpResponse .getCode () + ")" , httpResponse .getCode ());
0 commit comments