Fix issue where blank service shows as warning instead of error

Parse issues on service name still will show as warnings
This commit is contained in:
nsingh
2017-01-24 11:45:28 -08:00
parent 9312d6e6c6
commit 52129ab105
2 changed files with 32 additions and 22 deletions

View File

@@ -26,7 +26,6 @@ public class EnumValueParser implements ValueParser {
private String typeName;
private Provider<Collection<String>> 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<String> values) {
this(typeName, provider(values));
}
public EnumValueParser(String typeName, Callable<Collection<String>> values) {
this(typeName, provider(values));
}
public EnumValueParser(String typeName, Provider<Collection<String>> 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<String> 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<String> 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 <T> Provider<T> provider(T values) {
return () -> values;
}
private static <T> Provider<T> provider(Callable<T> values) {
return () -> {
try {

View File

@@ -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);
}
}