Skip to content

Commit

Permalink
Fix missing format strings in calls to DruidException.build (#15056)
Browse files Browse the repository at this point in the history
* Fix the NPE bug in nonStrictFormat

* using non null format string

* using Assert.assertThrows
  • Loading branch information
pranavbhole authored Sep 30, 2023
1 parent 86087ce commit 07c28f1
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public Response doPost(final SqlQuery sqlQuery, @Context final HttpServletReques
return buildNonOkResponse(
DruidException.forPersona(DruidException.Persona.DEVELOPER)
.ofCategory(DruidException.Category.UNCATEGORIZED)
.build(e.getMessage())
.build("%s", e.getMessage())
);
}
finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public Response doPost(
sqlQueryId,
DruidException.forPersona(DruidException.Persona.DEVELOPER)
.ofCategory(DruidException.Category.UNCATEGORIZED)
.build(e.getMessage())
.build("%s", e.getMessage())
);
}
finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public static Optional<SqlStatementResult> getExceptionPayload(
null,
DruidException.forPersona(DruidException.Persona.DEVELOPER)
.ofCategory(DruidException.Category.UNCATEGORIZED)
.build(taskResponse.getStatus().getErrorMsg()).toErrorResponse()
.build("%s", taskResponse.getStatus().getErrorMsg()).toErrorResponse()
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected DruidException makeException(DruidException.DruidExceptionBuilder bob)
{
return bob.forPersona(cause.getTargetPersona())
.ofCategory(cause.getCategory())
.build(cause, cause.getMessage())
.build(cause, "%s", cause.getMessage())
.withContext(cause.getContext());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected DruidException makeException(DruidException.DruidExceptionBuilder bob)
{
return bob.forPersona(DruidException.Persona.OPERATOR)
.ofCategory(convertFailType(exception.getFailType()))
.build(exception, exception.getMessage())
.build(exception, "%s", exception.getMessage())
.withContext("host", exception.getHost())
.withContext("errorClass", exception.getErrorClass())
.withContext("legacyErrorCode", exception.getErrorCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,32 @@ public void testQueryExceptionCompat()
.expectMessageIs("Query did not complete within configured timeout period. You can increase query timeout or tune the performance of query.")
);
}
@Test
public void testQueryExceptionCompatWithNullMessage()
{
ErrorResponse response = new ErrorResponse(DruidException.fromFailure(new QueryExceptionCompat(new QueryTimeoutException(
null,
"hostname"
))));
final Map<String, Object> asMap = response.getAsMap();
MatcherAssert.assertThat(
asMap,
DruidMatchers.mapMatcher(
"error",
"Query timeout",

"errorCode",
"legacyQueryException",

"persona",
"OPERATOR",

"category",
"TIMEOUT",

"errorMessage",
"null"
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,19 @@ public void testJavaStringCompare()
}
}
}

@Test()
public void testNonStrictFormatWithNullMessage()
{
Assert.assertThrows(NullPointerException.class, () -> StringUtils.nonStrictFormat(null, 1, 2));
}

@Test
public void testNonStrictFormatWithStringContainingPercent()
{
Assert.assertEquals(
"some string containing % %s %d %f",
StringUtils.nonStrictFormat("%s", "some string containing % %s %d %f")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public static DruidException translateException(Exception e)
return DruidException.forPersona(DruidException.Persona.USER)
.ofCategory(DruidException.Category.INVALID_INPUT)
.withErrorCode("invalidInput")
.build(inner, inner.getMessage()).withContext("sourceType", "sql");
.build(inner, "%s", inner.getMessage()).withContext("sourceType", "sql");
} else {
final String theUnexpectedToken = getUnexpectedTokenString(parseException);

Expand Down Expand Up @@ -390,14 +390,14 @@ public static DruidException translateException(Exception e)
catch (RelOptPlanner.CannotPlanException inner) {
return DruidException.forPersona(DruidException.Persona.USER)
.ofCategory(DruidException.Category.INVALID_INPUT)
.build(inner, inner.getMessage());
.build(inner, "%s", inner.getMessage());
}
catch (Exception inner) {
// Anything else. Should not get here. Anything else should already have
// been translated to a DruidException unless it is an unexpected exception.
return DruidException.forPersona(DruidException.Persona.ADMIN)
.ofCategory(DruidException.Category.UNCATEGORIZED)
.build(inner, inner.getMessage());
.build(inner, "%s", inner.getMessage());
}
}

Expand Down

0 comments on commit 07c28f1

Please sign in to comment.