From f56ec76a606844f8226ba9b4bddad13215a7707a Mon Sep 17 00:00:00 2001 From: Kai Sun Date: Fri, 20 Oct 2023 14:32:41 -0700 Subject: [PATCH] add unit test and limit error message length to 192 based on feedback --- .../druid/client/JsonParserIterator.java | 15 +++++---- .../druid/client/JsonParserIteratorTest.java | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/org/apache/druid/client/JsonParserIterator.java b/server/src/main/java/org/apache/druid/client/JsonParserIterator.java index 4bd749900d3d..2bb189e9b9f8 100644 --- a/server/src/main/java/org/apache/druid/client/JsonParserIterator.java +++ b/server/src/main/java/org/apache/druid/client/JsonParserIterator.java @@ -185,14 +185,15 @@ private void init() } else if (nextToken == JsonToken.START_OBJECT) { throw convertException(jp.getCodec().readValue(jp, QueryException.class)); } else { - if (nextToken == JsonToken.VALUE_STRING) { - LOG.error( - "Next Token value is of type VALUE_STRING with content [%s], not as expected START_ARRAY", - jp.getText() - ); - } + String errMsg = jp.getValueAsString(); + errMsg = errMsg.substring(0, Math.min(errMsg.length(), 192)); throw convertException( - new IAE("Next token wasn't a START_ARRAY, was[%s] from url[%s]", jp.getCurrentToken(), url) + new IAE( + "Next token wasn't a START_ARRAY, was[%s] from url[%s] with value[%s]", + jp.getCurrentToken(), + url, + errMsg + ) ); } } diff --git a/server/src/test/java/org/apache/druid/client/JsonParserIteratorTest.java b/server/src/test/java/org/apache/druid/client/JsonParserIteratorTest.java index 3ac2b4122d29..1b9af0cd6019 100644 --- a/server/src/test/java/org/apache/druid/client/JsonParserIteratorTest.java +++ b/server/src/test/java/org/apache/druid/client/JsonParserIteratorTest.java @@ -316,8 +316,41 @@ private Query mockQuery(String queryId, long timeoutAt) } } + public static class IAEExceptionConversionTest + { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private String meshErrMsg = "pstream connect error or disconnect/reset before header"; + + @Test + public void testMeshProxyError() + throws JsonProcessingException + { + JsonParserIterator iterator = new JsonParserIterator<>( + JAVA_TYPE, + Futures.immediateFuture( + mockMeshProxyResponse(meshErrMsg) + ), + URL, + null, + HOST, + OBJECT_MAPPER + ); + + expectedException.expect(QueryInterruptedException.class); + expectedException.expectMessage(meshErrMsg); + iterator.hasNext(); + } + } + private static InputStream mockErrorResponse(Exception e) throws JsonProcessingException { return new ByteArrayInputStream(OBJECT_MAPPER.writeValueAsBytes(e)); } + + private static InputStream mockMeshProxyResponse(String errMsg) throws JsonProcessingException + { + return new ByteArrayInputStream(OBJECT_MAPPER.writeValueAsBytes(errMsg)); + } }