Skip to content

Commit

Permalink
add unit test and limit error message length to 192 based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Sun committed Oct 20, 2023
1 parent aaf01af commit f56ec76
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,41 @@ private Query<?> mockQuery(String queryId, long timeoutAt)
}
}

public static class IAEExceptionConversionTest
{
@Rule
public ExpectedException expectedException = ExpectedException.none();

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note test

Invoking
ExpectedException.none
should be avoided because it has been deprecated.

private String meshErrMsg = "pstream connect error or disconnect/reset before header";

@Test
public void testMeshProxyError()
throws JsonProcessingException
{
JsonParserIterator<Object> 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));
}
}

0 comments on commit f56ec76

Please sign in to comment.