Skip to content

Commit

Permalink
Fix formatting in PropertyReferenceException message.
Browse files Browse the repository at this point in the history
A message like, "No property 'creat' found for type 'User' Did you mean ''created''" is now properly formatted as:
"No property 'creat' found for type 'User'; Did you mean 'created'".

Closes gh-2603.
  • Loading branch information
jxblum committed Dec 12, 2022
1 parent f8d6c9f commit bcc0968
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author John Blum
*/
public class PropertyReferenceException extends RuntimeException {

private static final long serialVersionUID = -5254424051438976570L;
private static final String ERROR_TEMPLATE = "No property '%s' found for type '%s'";
private static final String HINTS_TEMPLATE = " Did you mean '%s'";

static final String ERROR_TEMPLATE = "No property '%s' found for type '%s'";
static final String HINTS_TEMPLATE = "Did you mean %s";

private final String propertyName;
private final TypeInformation<?> type;
Expand Down Expand Up @@ -101,13 +103,13 @@ public String getMessage() {
Collection<String> potentialMatches = getPropertyMatches();
if (!potentialMatches.isEmpty()) {
String matches = StringUtils.collectionToDelimitedString(potentialMatches, ",", "'", "'");
builder.append("; ");
builder.append(String.format(HINTS_TEMPLATE, matches));
}

if (!alreadyResolvedPath.isEmpty()) {
builder.append(" Traversed path: ");
builder.append("; Traversed path: ");
builder.append(alreadyResolvedPath.get(0).toString());
builder.append(".");
}

return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author John Blum
*/
public class PropertyReferenceExceptionUnitTests {

Expand Down Expand Up @@ -65,6 +66,33 @@ public void exposesPotentialMatch() {
assertThat(matches).containsExactly("name");
}

@Test // GH-2750
public void formatsMessageWithTypeInfoAndHintsCorrectly() {

var exception = new PropertyReferenceException("nme", TYPE_INFO, NO_PATHS);

String expectedMessage = String.format("%s; %s", PropertyReferenceException.ERROR_TEMPLATE,
PropertyReferenceException.HINTS_TEMPLATE);

assertThat(exception)
.hasMessage(expectedMessage,"nme", TYPE_INFO.getType().getSimpleName(), "'name'");
}

@Test // GH-2750
public void formatsMessageWithTypeInfoHintsAndPathCorrectly() {

var ctype = TypeInformation.of(C.class);

var exception = new PropertyReferenceException("nme", TYPE_INFO,
Collections.singletonList(PropertyPath.from("b.a", ctype)));

String expectedMessage = String.format("%s; %s; %s", PropertyReferenceException.ERROR_TEMPLATE,
PropertyReferenceException.HINTS_TEMPLATE, "Traversed path: C.b.a");

assertThat(exception)
.hasMessage(expectedMessage,"nme", TYPE_INFO.getType().getSimpleName(), "'name'");
}

static class Sample {

String name;
Expand All @@ -77,4 +105,16 @@ public void setName(String name) {
this.name = name;
}
}

static class A {

}

static class B {
A a;
}

static class C {
B b;
}
}

0 comments on commit bcc0968

Please sign in to comment.