Skip to content

Commit

Permalink
using non null format string
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavbhole committed Sep 29, 2023
1 parent abec4a7 commit 45739b4
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 26 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 @@ -373,24 +373,16 @@ public static String nonStrictFormat(String message, Object... formatArgs)
if (formatArgs == null || formatArgs.length == 0) {
return message;
}
if (message == null) {
return compactFormatArgs("", formatArgs);
}
try {
return String.format(Locale.ENGLISH, message, formatArgs);
}
catch (IllegalFormatException e) {
return compactFormatArgs(message, formatArgs);
}
}

private static String compactFormatArgs(String message, Object... formatArgs)
{
StringBuilder bob = new StringBuilder(message);
for (Object formatArg : formatArgs) {
bob.append("; ").append(formatArg);
StringBuilder bob = new StringBuilder(message);
for (Object formatArg : formatArgs) {
bob.append("; ").append(formatArg);
}
return bob.toString();
}
return bob.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void testQueryExceptionCompatWithNullMessage()
"TIMEOUT",

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

@Test(expected = NullPointerException.class)
public void testNonStrictFormatWithNullMessage()
{
Assert.assertEquals("; 1; 2", StringUtils.nonStrictFormat(null, 1, 2));
Assert.assertEquals(null, StringUtils.nonStrictFormat(null));
Assert.assertEquals("", StringUtils.nonStrictFormat("", 1));
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 45739b4

Please sign in to comment.