diff --git a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/AlwaysFailingParser.java b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/AlwaysFailingParser.java index 41d4904f8..8a00aa39d 100644 --- a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/AlwaysFailingParser.java +++ b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/AlwaysFailingParser.java @@ -25,7 +25,7 @@ public class AlwaysFailingParser implements ValueParser { } @Override - public Object parse(String str) { + public Object parse(String str) throws Exception { throw new IllegalArgumentException("'"+str+"' is not valid for type '"+typeName+"'"); } 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 73a231735..a2541168c 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 @@ -44,11 +44,11 @@ public class EnumValueParser implements ValueParser { this.values = values; } - public Object parse(String str) { + 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 if (!StringUtil.hasText(str)) { - throw new IllegalArgumentException(createBlankTextErrorMessage()); + throw toError(createBlankTextErrorMessage()); } Collection values = this.values.get(); @@ -57,9 +57,13 @@ public class EnumValueParser implements ValueParser { if (values==null || values.contains(str)) { return str; } else { - throw new IllegalArgumentException(createErrorMessage(str, values)); + throw toError(createErrorMessage(str, values)); } } + + protected Exception toError(String message) throws Exception{ + return ExceptionUtil.asValueParseException(message); + } protected String createBlankTextErrorMessage() { return "'"+typeName+"'" + " cannot be blank."; diff --git a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ExceptionUtil.java b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ExceptionUtil.java index b242cf64b..9b6f7c723 100644 --- a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ExceptionUtil.java +++ b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ExceptionUtil.java @@ -61,22 +61,26 @@ public class ExceptionUtil { // The message of nested exception is usually more interesting than the // one on top. Throwable cause = getDeepestCause(e); - String msg = cause.getClass().getSimpleName() + ": " + cause.getMessage(); - return msg; + // If value parse exception, do not append any additional information + if (cause instanceof ValueParseException) { + String msg = cause.getMessage(); + if (StringUtil.hasText(msg)) { + return msg; + } else { + return "An error occurred: " + getSimpleError(cause); + } + } else if (cause != null) { + String msg = getSimpleError(cause) + ": " + cause.getMessage(); + return msg; + } else { + return "An error occurred: " + getSimpleError(e); + } + } + + private static String getSimpleError(Throwable e) { + return e.getClass().getSimpleName(); } - /** - * - * @param e - * @return only the message in the error without any appended information - */ - public static String getMessageOnly(Throwable e) { - // The message of nested exception is usually more interesting than the - // one on top. - Throwable cause = getDeepestCause(e); - String msg = cause.getMessage(); - return msg; - } public static IllegalStateException notImplemented(String string) { return new IllegalStateException("Not implemented: " + string); @@ -131,4 +135,8 @@ public class ExceptionUtil { return exception(error); } } + + public static Exception asValueParseException(String message) { + return new ValueParseException(message); + } } diff --git a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/RegexpParser.java b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/RegexpParser.java index 938121eae..fbf89b73c 100644 --- a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/RegexpParser.java +++ b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/RegexpParser.java @@ -36,7 +36,7 @@ public class RegexpParser implements ValueParser { } @Override - public Object parse(String str) { + public Object parse(String str) throws Exception { Matcher matcher = pat.matcher(str); if (matcher.matches()) { return matcher; diff --git a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParseException.java b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParseException.java new file mode 100644 index 000000000..c4da05427 --- /dev/null +++ b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParseException.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2017 Pivotal, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Pivotal, Inc. - initial API and implementation + *******************************************************************************/ +package org.springframework.ide.vscode.commons.util; + +/** + * Exception if there is a failure when parsing a value. It does not wrap + * other exceptions such that when thrown, the parse exception is the "deepest" + * error. + * + */ +public class ValueParseException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public ValueParseException(String message) { + super(message); + } + +} diff --git a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParser.java b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParser.java index 1ee05bb02..d06881746 100644 --- a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParser.java +++ b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParser.java @@ -22,5 +22,5 @@ public interface ValueParser { * May either return null, or throw an {@link IllegalArgumentException} to indicate * that the String is not the format this parser expects. */ - Object parse(String str); + Object parse(String str) throws Exception; } \ No newline at end of file diff --git a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParsers.java b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParsers.java index 2dde3ddb8..2662440d9 100644 --- a/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParsers.java +++ b/vscode-extensions/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/ValueParsers.java @@ -35,7 +35,7 @@ public class ValueParsers { Assert.isLegal(lowerBound==null || upperBound==null || lowerBound <= upperBound); return new ValueParser() { @Override - public Object parse(String str) { + public Object parse(String str) throws Exception { int value = Integer.parseInt(str); if (lowerBound!=null && value completions = new ArrayList<>(); diff --git a/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/AbstractCFHintsProvider.java b/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/AbstractCFHintsProvider.java index aae039e72..734d76ed6 100644 --- a/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/AbstractCFHintsProvider.java +++ b/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/AbstractCFHintsProvider.java @@ -47,7 +47,7 @@ public abstract class AbstractCFHintsProvider implements Callable values) { return "There is no service instance called '" + parseString + "'. Available service instances are: " + values; } + + protected String createBlankTextErrorMessage() { + return "At least one service instance name must be specified"; + } } diff --git a/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlValueParsers.java b/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlValueParsers.java index 92c2af451..897c04138 100644 --- a/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlValueParsers.java +++ b/vscode-extensions/vscode-manifest-yaml/src/main/java/org/springframework/ide/vscode/manifest/yaml/ManifestYmlValueParsers.java @@ -40,7 +40,7 @@ public class ManifestYmlValueParsers { private final Set UNITS = Sets.union(GIGABYTE, MEGABYTE); @Override - public Object parse(String str) { + public Object parse(String str) throws Exception { str = str.trim(); String unit = getUnit(str.toUpperCase()); if (unit==null) { @@ -75,7 +75,7 @@ public class ManifestYmlValueParsers { Assert.isLegal(lowerBound==null || upperBound==null || lowerBound <= upperBound); return new ValueParser() { @Override - public Object parse(String str) { + public Object parse(String str) throws Exception { int value = Integer.parseInt(str); if (lowerBound!=null && value