diff --git a/src/main/java/com/typesafe/config/ConfigException.java b/src/main/java/com/typesafe/config/ConfigException.java index 397baf2d..347f837b 100644 --- a/src/main/java/com/typesafe/config/ConfigException.java +++ b/src/main/java/com/typesafe/config/ConfigException.java @@ -129,7 +129,9 @@ public class ConfigException extends RuntimeException { public BadPath(ConfigOrigin origin, String path, String message, Throwable cause) { - super(origin, "Invalid path '" + path + "': " + message, cause); + super(origin, + path != null ? ("Invalid path '" + path + "': " + message) + : message, cause); } public BadPath(ConfigOrigin origin, String path, String message) { @@ -137,7 +139,8 @@ public class ConfigException extends RuntimeException { } public BadPath(String path, String message, Throwable cause) { - super("Invalid path '" + path + "': " + message, cause); + super(path != null ? ("Invalid path '" + path + "': " + message) + : message, cause); } public BadPath(String path, String message) { @@ -145,7 +148,7 @@ public class ConfigException extends RuntimeException { } public BadPath(ConfigOrigin origin, String message) { - super(origin, message); + this(origin, null, message); } } diff --git a/src/main/java/com/typesafe/config/impl/Parser.java b/src/main/java/com/typesafe/config/impl/Parser.java index a93e7002..b0c68b10 100644 --- a/src/main/java/com/typesafe/config/impl/Parser.java +++ b/src/main/java/com/typesafe/config/impl/Parser.java @@ -578,6 +578,11 @@ final class Parser { this.canBeEmpty = canBeEmpty; this.sb = new StringBuilder(initial); } + + @Override + public String toString() { + return "Element(" + sb.toString() + "," + canBeEmpty + ")"; + } } private static void addPathText(List buf, boolean wasQuoted, @@ -603,12 +608,18 @@ final class Parser { private static Path parsePathExpression(Iterator expression, ConfigOrigin origin) { + return parsePathExpression(expression, origin, null); + } + + // originalText may be null if not available + private static Path parsePathExpression(Iterator expression, + ConfigOrigin origin, String originalText) { // each builder in "buf" is an element in the path. List buf = new ArrayList(); buf.add(new Element("", false)); if (!expression.hasNext()) { - throw new ConfigException.BadPath(origin, "", + throw new ConfigException.BadPath(origin, originalText, "Expecting a field name or path here, but got nothing"); } @@ -643,7 +654,7 @@ final class Parser { } else if (Tokens.isUnquotedText(t)) { text = Tokens.getUnquotedText(t); } else { - throw new ConfigException.BadPath(origin, + throw new ConfigException.BadPath(origin, originalText, "Token not allowed in path expression: " + t); } @@ -657,8 +668,8 @@ final class Parser { if (e.sb.length() == 0 && !e.canBeEmpty) { throw new ConfigException.BadPath( origin, - buf.toString(), - "path has a leading, trailing, or two adjacent period '.' (use \"\" empty string if you want an empty element)"); + originalText, + "path has a leading, trailing, or two adjacent period '.' (use quoted \"\" empty string if you want an empty element)"); } else { pb.appendKey(e.sb.toString()); } @@ -680,7 +691,7 @@ final class Parser { Iterator tokens = Tokenizer.tokenize(apiOrigin, reader, ConfigSyntax.CONF); tokens.next(); // drop START - return parsePathExpression(tokens, apiOrigin); + return parsePathExpression(tokens, apiOrigin, path); } finally { reader.close(); } diff --git a/src/main/java/com/typesafe/config/impl/Tokenizer.java b/src/main/java/com/typesafe/config/impl/Tokenizer.java index da9487ec..1298f3c1 100644 --- a/src/main/java/com/typesafe/config/impl/Tokenizer.java +++ b/src/main/java/com/typesafe/config/impl/Tokenizer.java @@ -297,7 +297,8 @@ final class Tokenizer { } } catch (NumberFormatException e) { throw parseError("Invalid number: '" + s - + "' (if this is in a path, try quoting it)", e); + + "' (if this is in a path, try quoting it with double quotes)", + e); } }