[ 
https://issues.apache.org/jira/browse/BEAM-5141?focusedWorklogId=140173&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-140173
 ]

ASF GitHub Bot logged work on BEAM-5141:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 31/Aug/18 17:11
            Start Date: 31/Aug/18 17:11
    Worklog Time Spent: 10m 
      Work Description: amaliujia closed pull request #6216: [BEAM-5141] 
Improve error message when SET unregistered options
URL: https://github.com/apache/beam/pull/6216
 
 
   

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/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionUnexpectedPropertyException.java
 
b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionUnexpectedPropertyException.java
new file mode 100644
index 00000000000..efc0b272274
--- /dev/null
+++ 
b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionUnexpectedPropertyException.java
@@ -0,0 +1,77 @@
+/*
+ * 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.beam.sdk.options;
+
+import com.google.common.collect.Iterables;
+import java.util.SortedSet;
+
+/** An exception used when encounter unexpected properties. */
+public class PipelineOptionUnexpectedPropertyException extends 
IllegalArgumentException {
+  private static final long serialVersionUID = 3265630128856068164L;
+
+  private final String propertyName;
+  private final SortedSet<String> closestMatches;
+
+  public static PipelineOptionUnexpectedPropertyException create(
+      Class optionClass, String propertyName, SortedSet<String> 
closestMatches) {
+    String errorMessage = createErrorMessage(optionClass, propertyName, 
closestMatches);
+    PipelineOptionUnexpectedPropertyException exception =
+        new PipelineOptionUnexpectedPropertyException(errorMessage, 
propertyName, closestMatches);
+    return exception;
+  }
+
+  private static String createErrorMessage(
+      Class optionClass, String propertyName, SortedSet<String> 
closestMatches) {
+    String ret;
+
+    switch (closestMatches.size()) {
+      case 0:
+        ret = String.format("Class %s missing a property named '%s'.", 
optionClass, propertyName);
+        break;
+      case 1:
+        ret =
+            String.format(
+                "Class %s missing a property named '%s'. Did you mean '%s'?",
+                optionClass, propertyName, 
Iterables.getOnlyElement(closestMatches));
+        break;
+      default:
+        ret =
+            String.format(
+                "Class %s missing a property named '%s'. Did you mean one of 
%s?",
+                optionClass, propertyName, closestMatches);
+        break;
+    }
+
+    return ret;
+  }
+
+  private PipelineOptionUnexpectedPropertyException(
+      String errorMessage, String propertyName, SortedSet<String> 
closestMatches) {
+    super(errorMessage);
+    this.propertyName = propertyName;
+    this.closestMatches = closestMatches;
+  }
+
+  public String getPropertyName() {
+    return propertyName;
+  }
+
+  public SortedSet<String> getClosestMatches() {
+    return closestMatches;
+  }
+}
diff --git 
a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java
 
b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java
index 0a1e70cc529..db3c3d2e779 100644
--- 
a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java
+++ 
b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java
@@ -1512,21 +1512,9 @@ public int compare(Method o1, Method o2) {
                   Sets.filter(
                       propertyNamesToGetters.keySet(),
                       input -> 
StringUtils.getLevenshteinDistance(entry.getKey(), input) <= 2));
-          switch (closestMatches.size()) {
-            case 0:
-              throw new IllegalArgumentException(
-                  String.format("Class %s missing a property named '%s'.", 
klass, entry.getKey()));
-            case 1:
-              throw new IllegalArgumentException(
-                  String.format(
-                      "Class %s missing a property named '%s'. Did you mean 
'%s'?",
-                      klass, entry.getKey(), 
Iterables.getOnlyElement(closestMatches)));
-            default:
-              throw new IllegalArgumentException(
-                  String.format(
-                      "Class %s missing a property named '%s'. Did you mean 
one of %s?",
-                      klass, entry.getKey(), closestMatches));
-          }
+
+          throw PipelineOptionUnexpectedPropertyException.create(
+              klass, entry.getKey(), closestMatches);
         }
         Method method = propertyNamesToGetters.get(entry.getKey());
         // Only allow empty argument values for String, String Array, and 
Collection<String>.
diff --git 
a/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamEnumerableConverter.java
 
b/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamEnumerableConverter.java
index 77cbd8e8d11..ca3729edd24 100644
--- 
a/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamEnumerableConverter.java
+++ 
b/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamEnumerableConverter.java
@@ -19,6 +19,7 @@
 
 import static com.google.common.base.Preconditions.checkArgument;
 
+import com.google.common.collect.Iterables;
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
@@ -38,6 +39,7 @@
 import org.apache.beam.sdk.metrics.Metrics;
 import org.apache.beam.sdk.metrics.MetricsFilter;
 import org.apache.beam.sdk.options.ApplicationNameOptions;
+import org.apache.beam.sdk.options.PipelineOptionUnexpectedPropertyException;
 import org.apache.beam.sdk.options.PipelineOptions;
 import org.apache.beam.sdk.options.PipelineOptionsFactory;
 import org.apache.beam.sdk.runners.TransformHierarchy.Node;
@@ -118,7 +120,27 @@ public static PipelineOptions 
createPipelineOptions(Map<String, String> map) {
     for (Map.Entry<String, String> entry : map.entrySet()) {
       args[i++] = "--" + entry.getKey() + "=" + entry.getValue();
     }
-    PipelineOptions options = 
PipelineOptionsFactory.fromArgs(args).withValidation().create();
+
+    PipelineOptions options;
+    try {
+      options = 
PipelineOptionsFactory.fromArgs(args).withValidation().create();
+    } catch (PipelineOptionUnexpectedPropertyException e) {
+      String errorMessage =
+          String.format(
+              "Pipeline option '%s' is not supported in this context. It must 
be "
+                  + "cleared by 'RESET %s'.",
+              e.getPropertyName(), e.getPropertyName());
+
+      if (e.getClosestMatches().size() == 1) {
+        errorMessage +=
+            String.format(
+                " Did you mean 'SET %s'?", 
Iterables.getOnlyElement(e.getClosestMatches()));
+      } else if (e.getClosestMatches().size() > 1) {
+        errorMessage += String.format(" Did you mean SET one of %s?", 
e.getClosestMatches());
+      }
+      throw new IllegalArgumentException(errorMessage, e);
+    }
+
     options.as(ApplicationNameOptions.class).setAppName("BeamSql");
     return options;
   }


 

----------------------------------------------------------------
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:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 140173)
    Time Spent: 7h 50m  (was: 7h 40m)

> Improve error message when SET unregistered options 
> ----------------------------------------------------
>
>                 Key: BEAM-5141
>                 URL: https://issues.apache.org/jira/browse/BEAM-5141
>             Project: Beam
>          Issue Type: Improvement
>          Components: dsl-sql
>            Reporter: Rui Wang
>            Assignee: Rui Wang
>            Priority: Major
>             Fix For: Not applicable
>
>          Time Spent: 7h 50m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to