Improve Exceptions' toString() method

This commit is contained in:
Zafar Khaja 2014-01-28 17:15:32 +04:00
parent da3ce7c65f
commit f95c18cf6c
3 changed files with 19 additions and 6 deletions

View File

@ -85,9 +85,16 @@ public class UnexpectedCharacterException extends ParseException {
*/
@Override
public String toString() {
String message = "Unexpected character '" + unexpected + "'";
String message = String.format(
"Unexpected character '%s(%s)'",
CharType.forCharacter(unexpected),
unexpected
);
if (expected.length > 0) {
message += ", expecting '" + Arrays.toString(expected) + "'";
message += String.format(
", expecting '%s'",
Arrays.toString(expected)
);
}
return message;
}

View File

@ -66,9 +66,12 @@ public class UnexpectedTokenException extends ParseException {
*/
@Override
public String toString() {
String message = "Unexpected token '" + unexpected + "'";
String message = String.format("Unexpected token '%s'", unexpected);
if (expected.length > 0) {
message += ", expecting '" + Arrays.toString(expected) + "'";
message += String.format(
", expecting '%s'",
Arrays.toString(expected)
);
}
return message;
}

View File

@ -84,9 +84,12 @@ public class UnexpectedElementException extends RuntimeException {
*/
@Override
public String toString() {
String message = "Unexpected element '" + unexpected + "'";
String message = String.format("Unexpected element '%s'", unexpected);
if (expected.length > 0) {
message += ", expecting '" + Arrays.toString(expected) + "'";
message += String.format(
", expecting '%s'",
Arrays.toString(expected)
);
}
return message;
}