geruh commented on code in PR #12564:
URL: https://github.com/apache/iceberg/pull/12564#discussion_r2377050769
##########
core/src/main/java/org/apache/iceberg/SerializableTable.java:
##########
@@ -265,14 +267,15 @@ public EncryptionManager encryption() {
@Override
public LocationProvider locationProvider() {
Review Comment:
Can we add some tests in spark to ensure that the exception isn't thrown on
instantiation but when `serializableTable.locationProvider()` is thrown
##########
core/src/main/java/org/apache/iceberg/SerializableTable.java:
##########
@@ -265,14 +267,15 @@ public EncryptionManager encryption() {
@Override
public LocationProvider locationProvider() {
- if (lazyLocationProvider == null) {
- synchronized (this) {
- if (lazyLocationProvider == null) {
- this.lazyLocationProvider = LocationProviders.locationsFor(location,
properties);
- }
+ try {
Review Comment:
We can move this logic into the Try class as `public T getOrThrow()`
##########
core/src/test/java/org/apache/iceberg/util/TestTryUtil.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import org.junit.Test;
Review Comment:
```suggestion
import org.junit.jupiter.api.Test;
```
##########
core/src/main/java/org/apache/iceberg/util/TryUtil.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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.util;
+
+import java.io.Serializable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
+/** Utility for executing code that may throw exceptions and deferring
exception handling. */
+public class TryUtil {
+ private TryUtil() {}
+
+ /**
+ * Functional interface for operations that may throw exceptions.
+ *
+ * @param <T> return type of the operation
+ */
+ @FunctionalInterface
+ public interface ThrowingSupplier<T> {
+ T get() throws Exception;
+ }
+
+ /**
+ * Executes the given operation and returns a Try object containing either
the result or the
+ * exception.
+ *
+ * @param operation the operation to execute
+ * @param <T> the type of the result
+ * @return a Try object containing either the result or the exception
+ */
+ public static <T> Try<T> run(ThrowingSupplier<T> operation) {
+ try {
+ return Try.success(operation.get());
+ } catch (Exception e) {
+ return Try.failure(e);
+ }
+ }
+
+ /**
+ * Container for the result of an operation that might throw an exception.
+ *
+ * @param <T> the type of the result
+ */
+ public static class Try<T> implements Serializable {
Review Comment:
Instead of using a util class what do you think about adding a static helper
like:
```
public static <T> Try<T> capture(ThrowingSupplier<T> supplier) {
try {
return success(supplier.get());
} catch (Exception e) {
return failure(e);
}
}
```
Then usage would look like
`Try<LocationProvider> lp = Try.capture(table::locationProvider);`
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]