You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by ao...@apache.org on 2021/03/24 18:41:20 UTC

[iceberg] 05/18: Core: Improve error messages in CatalogUtil.loadCatalog (#2146)

This is an automated email from the ASF dual-hosted git repository.

aokolnychyi pushed a commit to branch 0.11.x
in repository https://gitbox.apache.org/repos/asf/iceberg.git

commit c90d26cac41220c1a6a6140ff3814c671e076d04
Author: Ryan Murray <ry...@dremio.com>
AuthorDate: Thu Jan 28 20:54:19 2021 +0100

    Core: Improve error messages in CatalogUtil.loadCatalog (#2146)
---
 .../main/java/org/apache/iceberg/CatalogUtil.java  |  2 +-
 .../iceberg/TestCatalogErrorConstructor.java       | 70 ++++++++++++++++++++++
 .../java/org/apache/iceberg/TestCatalogUtil.java   | 28 ++++++++-
 3 files changed, 98 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/iceberg/CatalogUtil.java b/core/src/main/java/org/apache/iceberg/CatalogUtil.java
index f22996d..776cc2f 100644
--- a/core/src/main/java/org/apache/iceberg/CatalogUtil.java
+++ b/core/src/main/java/org/apache/iceberg/CatalogUtil.java
@@ -155,7 +155,7 @@ public class CatalogUtil {
       ctor = DynConstructors.builder(Catalog.class).impl(impl).buildChecked();
     } catch (NoSuchMethodException e) {
       throw new IllegalArgumentException(String.format(
-          "Cannot initialize Catalog, missing no-arg constructor: %s", impl), e);
+          "Cannot initialize Catalog implementation %s: %s", impl, e.getMessage()), e);
     }
 
     Catalog catalog;
diff --git a/core/src/test/java/org/apache/iceberg/TestCatalogErrorConstructor.java b/core/src/test/java/org/apache/iceberg/TestCatalogErrorConstructor.java
new file mode 100644
index 0000000..ade2dd1
--- /dev/null
+++ b/core/src/test/java/org/apache/iceberg/TestCatalogErrorConstructor.java
@@ -0,0 +1,70 @@
+/*
+ * 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.iceberg;
+
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+
+
+/**
+ * Throws an error on initialization to simulate class not found error.
+ */
+public class TestCatalogErrorConstructor extends BaseMetastoreCatalog {
+  static {
+    if (true) {
+      throw new NoClassDefFoundError("Error while initializing class");
+    }
+  }
+
+  public TestCatalogErrorConstructor() {
+
+  }
+
+  @Override
+  protected TableOperations newTableOps(TableIdentifier tableIdentifier) {
+    return null;
+  }
+
+  @Override
+  protected String defaultWarehouseLocation(TableIdentifier tableIdentifier) {
+    return null;
+  }
+
+  @Override
+  public List<TableIdentifier> listTables(Namespace namespace) {
+    return null;
+  }
+
+  @Override
+  public boolean dropTable(TableIdentifier identifier, boolean purge) {
+    return false;
+  }
+
+  @Override
+  public void renameTable(TableIdentifier from, TableIdentifier to) {
+
+  }
+
+  @Override
+  public void initialize(String name, Map<String, String> properties) {
+  }
+}
diff --git a/core/src/test/java/org/apache/iceberg/TestCatalogUtil.java b/core/src/test/java/org/apache/iceberg/TestCatalogUtil.java
index 84ab3d4..37d66c5 100644
--- a/core/src/test/java/org/apache/iceberg/TestCatalogUtil.java
+++ b/core/src/test/java/org/apache/iceberg/TestCatalogUtil.java
@@ -70,7 +70,7 @@ public class TestCatalogUtil {
     String name = "custom";
     AssertHelpers.assertThrows("must have no-arg constructor",
         IllegalArgumentException.class,
-        "missing no-arg constructor",
+        "NoSuchMethodException: org.apache.iceberg.TestCatalogUtil$TestCatalogBadConstructor.<init>()",
         () -> CatalogUtil.loadCatalog(TestCatalogBadConstructor.class.getName(), name, options, hadoopConf));
   }
 
@@ -87,6 +87,32 @@ public class TestCatalogUtil {
         () -> CatalogUtil.loadCatalog(TestCatalogNoInterface.class.getName(), name, options, hadoopConf));
   }
 
+  @Test
+  public void loadCustomCatalog_ConstructorErrorCatalog() {
+    Map<String, String> options = new HashMap<>();
+    options.put("key", "val");
+    Configuration hadoopConf = new Configuration();
+    String name = "custom";
+
+    String impl = TestCatalogErrorConstructor.class.getName();
+    AssertHelpers.assertThrows("must be able to initialize catalog",
+        IllegalArgumentException.class,
+        "NoClassDefFoundError: Error while initializing class",
+        () -> CatalogUtil.loadCatalog(impl, name, options, hadoopConf));
+  }
+
+  @Test
+  public void loadCustomCatalog_BadCatalogNameCatalog() {
+    Map<String, String> options = new HashMap<>();
+    options.put("key", "val");
+    Configuration hadoopConf = new Configuration();
+    String name = "custom";
+    String impl = "CatalogDoesNotExist";
+    AssertHelpers.assertThrows("catalog must exist",
+        IllegalArgumentException.class,
+        "java.lang.ClassNotFoundException: CatalogDoesNotExist",
+        () -> CatalogUtil.loadCatalog(impl, name, options, hadoopConf));
+  }
 
   @Test
   public void loadCustomFileIO_noArg() {