rdblue commented on a change in pull request #2146:
URL: https://github.com/apache/iceberg/pull/2146#discussion_r564746337
##########
File path: core/src/main/java/org/apache/iceberg/CatalogUtil.java
##########
@@ -129,6 +129,35 @@ private static void deleteFiles(FileIO io,
Set<ManifestFile> allManifests) {
});
}
+ /**
+ * Get constructor for Catalog from impl. Impl must be valid class name and
have zero-arg constructor.
+ *
+ * <p>We try to get the constructor for impl and if we fail we check for
common error patterns. Specifically:
+ * a ClassNotFoundException for an invalid impl String and a
NoClassDefFoundError if impl fails to get initialized.
+ * Otherwise the original exception is passed up and the error is assumed to
be a missing no-args constructor.
+ *
+ * @param impl catalog implementation full class name
+ * @return constructor for catalog implementation impl
+ * @throws IllegalArgumentException if no-arg constructor not found or error
during initialization
+ */
+ private static DynConstructors.Ctor<Catalog> getCatalogConstructor(String
impl) {
+ DynConstructors.Ctor<Catalog> ctor;
+ DynConstructors.Builder ctorBuilder =
DynConstructors.builder(Catalog.class).impl(impl);
+ try {
+ ctor = ctorBuilder.buildChecked();
+ } catch (NoSuchMethodException e) {
+ Throwable originalError = ctorBuilder.problems().getOrDefault(impl, e);
+ String message = String.format("missing no-arg constructor: %s", impl);
+ if (originalError instanceof ClassNotFoundException) {
+ message = String.format("catalog class %s not found", impl);
+ } else if (originalError instanceof NoClassDefFoundError) {
+ message = String.format("catalog %s cannot be initialized", impl);
+ }
+ throw new IllegalArgumentException(String.format("Cannot initialize
Catalog, %s", message), originalError);
+ }
Review comment:
I don't get why this doesn't use the error message with all of the
problems listed. Wouldn't that be easier? The original `try` could be used,
like this:
```java
try {
ctor =
DynConstructors.builder(Catalog.class).impl(impl).buildChecked();
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(String.format(
"Cannot initialize Catalog implementation %s: %s", impl,
e.getMessage()), e);
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]