@@ -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