jerrypeng closed pull request #1853: improving error messages when loading 
configs
URL: https://github.com/apache/incubator-pulsar/pull/1853
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
 
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
index e726cce919..a200de8ed9 100644
--- 
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
+++ 
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
@@ -39,6 +39,7 @@
 import org.apache.bookkeeper.clients.config.StorageClientSettings;
 import org.apache.bookkeeper.clients.utils.NetUtils;
 import org.apache.commons.lang.StringUtils;
+import org.apache.pulsar.admin.cli.utils.CmdUtils;
 import org.apache.pulsar.client.admin.PulsarAdmin;
 import org.apache.pulsar.client.admin.internal.FunctionsImpl;
 import org.apache.pulsar.client.api.PulsarClientException;
@@ -259,7 +260,7 @@ void processArguments() throws Exception {
 
             // Initialize config builder either from a supplied YAML config 
file or from scratch
             if (null != fnConfigFile) {
-                functionConfig = Utils.loadConfig(fnConfigFile, 
FunctionConfig.class);
+                functionConfig = CmdUtils.loadConfig(fnConfigFile, 
FunctionConfig.class);
             } else {
                 functionConfig = new FunctionConfig();
             }
diff --git 
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSinks.java 
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSinks.java
index 607a4d239e..14a6fb107f 100644
--- 
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSinks.java
+++ 
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSinks.java
@@ -25,6 +25,7 @@
 import com.google.gson.Gson;
 import com.google.gson.reflect.TypeToken;
 import lombok.Getter;
+import org.apache.pulsar.admin.cli.utils.CmdUtils;
 import org.apache.pulsar.client.admin.PulsarAdmin;
 import org.apache.pulsar.client.admin.internal.FunctionsImpl;
 import org.apache.pulsar.functions.api.utils.IdentityFunction;
@@ -53,7 +54,6 @@
 import static 
org.apache.pulsar.functions.utils.Utils.convertProcessingGuarantee;
 import static org.apache.pulsar.functions.utils.Utils.fileExists;
 import static org.apache.pulsar.functions.utils.Utils.getSinkType;
-import static org.apache.pulsar.functions.utils.Utils.loadConfig;
 
 @Getter
 @Parameters(commandDescription = "Interface for managing Pulsar Sinks (Egress 
data from Pulsar)")
@@ -168,7 +168,7 @@ void processArguments() throws Exception {
             super.processArguments();
 
             if (null != sinkConfigFile) {
-                this.sinkConfig = loadConfig(sinkConfigFile, SinkConfig.class);
+                this.sinkConfig = CmdUtils.loadConfig(sinkConfigFile, 
SinkConfig.class);
             } else {
                 this.sinkConfig = new SinkConfig();
             }
diff --git 
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSources.java 
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSources.java
index ffc48917c4..b604d66077 100644
--- 
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSources.java
+++ 
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSources.java
@@ -25,6 +25,7 @@
 import com.google.gson.Gson;
 import com.google.gson.reflect.TypeToken;
 import lombok.Getter;
+import org.apache.pulsar.admin.cli.utils.CmdUtils;
 import org.apache.pulsar.client.admin.PulsarAdmin;
 import org.apache.pulsar.client.admin.internal.FunctionsImpl;
 import org.apache.pulsar.functions.api.utils.IdentityFunction;
@@ -49,7 +50,6 @@
 import static 
org.apache.pulsar.functions.utils.Utils.convertProcessingGuarantee;
 import static org.apache.pulsar.functions.utils.Utils.fileExists;
 import static org.apache.pulsar.functions.utils.Utils.getSourceType;
-import static org.apache.pulsar.functions.utils.Utils.loadConfig;
 
 @Getter
 @Parameters(commandDescription = "Interface for managing Pulsar Source 
(Ingress data to Pulsar)")
@@ -163,7 +163,7 @@ void processArguments() throws Exception {
             super.processArguments();
 
             if (null != sourceConfigFile) {
-                this.sourceConfig = loadConfig(sourceConfigFile, 
SourceConfig.class);
+                this.sourceConfig = CmdUtils.loadConfig(sourceConfigFile, 
SourceConfig.class);
             } else {
                 this.sourceConfig = new SourceConfig();
             }
diff --git 
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/utils/CmdUtils.java
 
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/utils/CmdUtils.java
new file mode 100644
index 0000000000..c86ea42301
--- /dev/null
+++ 
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/utils/CmdUtils.java
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.admin.cli.utils;
+
+import com.beust.jcommander.ParameterException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
+import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+
+import java.io.File;
+import java.io.IOException;
+
+public class CmdUtils {
+    public static <T> T loadConfig(String file, Class<T> clazz) throws 
IOException {
+        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
+        try {
+            return mapper.readValue(new File(file), clazz);
+        } catch (Exception ex) {
+            if (ex instanceof UnrecognizedPropertyException) {
+                UnrecognizedPropertyException unrecognizedPropertyException
+                        = (UnrecognizedPropertyException) ex;
+
+                String exceptionMessage = String.format("Failed to parse 
config file %s. "
+                                + "Invalid field '%s' on line: %d column: %d. 
Valid fields are %s",
+                        file,
+                        
unrecognizedPropertyException.getPath().get(0).getFieldName(),
+                        
unrecognizedPropertyException.getLocation().getLineNr(),
+                        
unrecognizedPropertyException.getLocation().getColumnNr(),
+                        unrecognizedPropertyException.getKnownPropertyIds());
+                throw new ParameterException(exceptionMessage);
+            } else if(ex instanceof InvalidFormatException) {
+
+                InvalidFormatException invalidFormatException = 
(InvalidFormatException) ex;
+                String exceptionMessage = String.format("Failed to parse 
config file %s. %s on line: %d column: %d",
+                        file,
+                        invalidFormatException.getOriginalMessage(),
+                        invalidFormatException.getLocation().getLineNr(),
+                        invalidFormatException.getLocation().getColumnNr());
+
+                throw new ParameterException(exceptionMessage);
+            } else {
+                throw new ParameterException(ex.getMessage());
+            }
+        }
+    }
+}
diff --git 
a/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/sink/PulsarSink.java
 
b/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/sink/PulsarSink.java
index 8b246cf78c..0356ab1d94 100644
--- 
a/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/sink/PulsarSink.java
+++ 
b/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/sink/PulsarSink.java
@@ -44,7 +44,6 @@
 
 import java.util.Base64;
 import java.util.Map;
-import java.util.concurrent.CompletableFuture;
 
 @Slf4j
 public class PulsarSink<T> implements Sink<T> {
diff --git 
a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/Utils.java
 
b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/Utils.java
index c534ed3db3..2601ca0aeb 100644
--- 
a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/Utils.java
+++ 
b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/Utils.java
@@ -18,8 +18,6 @@
  */
 package org.apache.pulsar.functions.utils;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
 import com.google.protobuf.AbstractMessage.Builder;
 import com.google.protobuf.MessageOrBuilder;
 import com.google.protobuf.util.JsonFormat;
@@ -154,11 +152,6 @@ public static Object createInstance(String userClassName, 
ClassLoader classLoade
 
     }
 
-    public static <T> T loadConfig(String file, Class<T> clazz) throws 
IOException {
-        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
-        return mapper.readValue(new File(file), clazz);
-    }
-
     public static Runtime convertRuntime(FunctionConfig.Runtime runtime) {
         for (Runtime type : Runtime.values()) {
             if (type.name().equals(runtime.name())) {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to