diff --git a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/EnumValueParser.java b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/EnumValueParser.java index bf668c693..70b20d49a 100644 --- a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/EnumValueParser.java +++ b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/EnumValueParser.java @@ -26,7 +26,6 @@ public class EnumValueParser implements ValueParser { private String typeName; private Provider> values; - public EnumValueParser(String typeName, String... values) { this(typeName, ImmutableSet.copyOf(values)); @@ -35,50 +34,54 @@ public class EnumValueParser implements ValueParser { public EnumValueParser(String typeName, Collection values) { this(typeName, provider(values)); } + public EnumValueParser(String typeName, Callable> values) { this(typeName, provider(values)); } - + public EnumValueParser(String typeName, Provider> values) { this.typeName = typeName; this.values = values; } - - public Object parse(String str) throws Exception { + + public Object parse(String str) throws Exception { // IMPORTANT: check the text FIRST before fetching values - // from the hints provider, as the hints provider may be expensive when resolving values + // from the hints provider, as the hints provider may be expensive when + // resolving values if (!StringUtil.hasText(str)) { - throw createException(createBlankTextErrorMessage()); + throw errorOnBlank(createBlankTextErrorMessage()); } - + Collection values = this.values.get(); - //If values is not known (null) then just assume the str is acceptable. - if (values==null || values.contains(str)) { + // If values is not known (null) then just assume the str is acceptable. + if (values == null || values.contains(str)) { return str; } else { - throw createException(createErrorMessage(str, values)); + throw errorOnParse(createErrorMessage(str, values)); } } - - - protected Exception createException(String message) { - return new IllegalArgumentException(message); - } protected String createBlankTextErrorMessage() { - return "'"+typeName+"'" + " cannot be blank."; + return "'" + typeName + "'" + " cannot be blank."; } protected String createErrorMessage(String parseString, Collection values) { - return "'"+parseString+"' is not valid for Enum '"+typeName+"'. Valid values are: "+values; + return "'" + parseString + "' is not valid for Enum '" + typeName + "'. Valid values are: " + values; } - - + + protected Exception errorOnParse(String message) { + return new IllegalArgumentException(message); + } + + protected Exception errorOnBlank(String message) { + return new IllegalArgumentException(message); + } + private static Provider provider(T values) { return () -> values; } - + private static Provider provider(Callable values) { return () -> { try { diff --git a/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/CFServicesValueParser.java b/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/CFServicesValueParser.java index fa5587d95..e3fe8e29a 100644 --- a/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/CFServicesValueParser.java +++ b/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/CFServicesValueParser.java @@ -15,6 +15,7 @@ import java.util.concurrent.Callable; import org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException; import org.springframework.ide.vscode.commons.util.EnumValueParser; +import org.springframework.ide.vscode.commons.yaml.reconcile.YamlSchemaProblems; public class CFServicesValueParser extends EnumValueParser { @@ -32,8 +33,14 @@ public class CFServicesValueParser extends EnumValueParser { return "At least one service instance name must be specified"; } - @Override - protected Exception createException(String message) { + protected Exception errorOnParse(String message) { + // Parse errors should be indicated differently than regular schema + // problems (e.g. unknown service may be a warning) return new ReconcileException(message, ManifestYamlSchemaProblemsTypes.UNKNOWN_SERVICES_PROBLEM); } + + protected Exception errorOnBlank(String message) { + // Blank errors should be regular schema problems + return new ReconcileException(message, YamlSchemaProblems.SCHEMA_PROBLEM); + } }