diff options
author | Jon Bratseth <bratseth@oath.com> | 2018-11-26 09:57:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-26 09:57:53 +0100 |
commit | 04cc3c48130b8397c04335948e5971914b2eaf22 (patch) | |
tree | d5bccb76d08794584f8ce9b5297ae5747a850d17 | |
parent | 87414164becdb1ffe3a29516734ab60947f597f0 (diff) | |
parent | 39a5fb33dada81891d9082e0a5d616efe62c4c06 (diff) |
Merge pull request #7763 from vespa-engine/hakonhall/ignore-nosuchfileexception-with-ifexists
Use yolean Exceptions instead of IOExceptionUtil
14 files changed, 81 insertions, 23 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/StorageMaintainer.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/StorageMaintainer.java index 6e3419813a7..2fd40a1b486 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/StorageMaintainer.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/StorageMaintainer.java @@ -36,7 +36,7 @@ import java.util.regex.Pattern; import static com.yahoo.vespa.hosted.node.admin.task.util.file.FileFinder.nameMatches; import static com.yahoo.vespa.hosted.node.admin.task.util.file.FileFinder.olderThan; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; +import static com.yahoo.yolean.Exceptions.uncheck; import static com.yahoo.vespa.hosted.node.admin.util.SecretAgentCheckConfig.nodeTypeToRole; /** diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/coredump/CoredumpHandler.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/coredump/CoredumpHandler.java index a9d61d20f5b..13a1311b86f 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/coredump/CoredumpHandler.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/coredump/CoredumpHandler.java @@ -23,7 +23,7 @@ import java.util.regex.Pattern; import static com.yahoo.vespa.hosted.node.admin.task.util.file.FileFinder.nameEndsWith; import static com.yahoo.vespa.hosted.node.admin.task.util.file.FileFinder.nameMatches; import static com.yahoo.vespa.hosted.node.admin.task.util.file.FileFinder.nameStartsWith; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; +import static com.yahoo.yolean.Exceptions.uncheck; /** * Finds coredumps, collects metadata and reports them diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java index 7dcae199858..bbc3427433b 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java @@ -13,7 +13,7 @@ import java.util.function.Consumer; import java.util.function.Supplier; import java.util.logging.Logger; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; +import static com.yahoo.yolean.Exceptions.uncheck; /** * An editor meant to edit small line-based files like /etc/fstab. diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/MakeDirectory.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/MakeDirectory.java index 2a88387f8fc..5f72ed7e9b8 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/MakeDirectory.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/MakeDirectory.java @@ -49,7 +49,7 @@ public class MakeDirectory { } } else { if (createParents) { - // We'll skip logginer system modification here, as we'll log about the creation + // We'll skip logging system modification here, as we'll log about the creation // of the directory next. path.createParents(); } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Template.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Template.java index 14fea240baa..cef35803e98 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Template.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Template.java @@ -9,6 +9,8 @@ import java.io.StringWriter; import java.nio.file.Files; import java.nio.file.Path; +import static com.yahoo.yolean.Exceptions.uncheck; + /** * Uses the Velocity engine to render a template, to and from both String and Path objects. * @@ -30,7 +32,7 @@ public class Template { } public static Template at(Path templatePath) { - return of(IOExceptionUtil.uncheck(() -> new String(Files.readAllBytes(templatePath)))); + return of(uncheck(() -> new String(Files.readAllBytes(templatePath)))); } public static Template of(String template) { diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java index 4bd3aad3f52..0b24a37af14 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java @@ -26,7 +26,8 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; +import static com.yahoo.yolean.Exceptions.uncheck; +import static com.yahoo.yolean.Exceptions.uncheckAndIgnore; /** * Thin wrapper around java.nio.file.Path, especially nice for UNIX-specific features. @@ -127,7 +128,7 @@ public class UnixPath { } public Optional<FileAttributes> getAttributesIfExists() { - return IOExceptionUtil.ifExists(this::getAttributes); + return Optional.ofNullable(uncheckAndIgnore(this::getAttributes, NoSuchFileException.class)); } public UnixPath createNewFile() { diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ChildProcess2Impl.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ChildProcess2Impl.java index 67020270a99..6c8b15b10a6 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ChildProcess2Impl.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ChildProcess2Impl.java @@ -12,7 +12,7 @@ import java.time.Instant; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; +import static com.yahoo.yolean.Exceptions.uncheck; /** * @author hakonhall diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ProcessFactoryImpl.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ProcessFactoryImpl.java index 1c7a60a13fc..78c25897e1d 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ProcessFactoryImpl.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ProcessFactoryImpl.java @@ -16,7 +16,7 @@ import java.util.List; import java.util.Set; import java.util.logging.Logger; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; +import static com.yahoo.yolean.Exceptions.uncheck; /** * @author hakonhall diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ProcessStarterImpl.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ProcessStarterImpl.java index 43105f43dc0..3d2caaa2ce9 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ProcessStarterImpl.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ProcessStarterImpl.java @@ -6,7 +6,7 @@ import com.yahoo.log.LogLevel; import java.util.logging.Logger; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; +import static com.yahoo.yolean.Exceptions.uncheck; /** * @author hakonhall diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/integrationTests/DockerTester.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/integrationTests/DockerTester.java index e22606104f1..db8520c63a8 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/integrationTests/DockerTester.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/integrationTests/DockerTester.java @@ -34,7 +34,7 @@ import java.util.Optional; import java.util.function.Function; import java.util.logging.Logger; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; +import static com.yahoo.yolean.Exceptions.uncheck; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/StorageMaintainerTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/StorageMaintainerTest.java index c8d5292889b..9ea5c87511b 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/StorageMaintainerTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/StorageMaintainerTest.java @@ -34,7 +34,7 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; +import static com.yahoo.yolean.Exceptions.uncheck; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/coredump/CoredumpHandlerTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/coredump/CoredumpHandlerTest.java index 8d599660ace..7779a74ae03 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/coredump/CoredumpHandlerTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/coredump/CoredumpHandlerTest.java @@ -30,7 +30,7 @@ import java.util.Set; import java.util.function.Supplier; import java.util.stream.Collectors; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck; +import static com.yahoo.yolean.Exceptions.uncheck; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.mockito.ArgumentMatchers.any; diff --git a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java index 82677a14242..fa3eb412016 100644 --- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java +++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java @@ -3,6 +3,9 @@ package com.yahoo.yolean; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.file.NoSuchFileException; +import java.util.Optional; +import java.util.function.Supplier; /** * Helper methods for handling exceptions @@ -70,6 +73,29 @@ public class Exceptions { } } + /** Similar to uncheck(), except an exceptionToIgnore exception is silently ignored. */ + public static <T extends IOException> void uncheckAndIgnore(RunnableThrowingIOException runnable, Class<T> exceptionToIgnore) { + try { + runnable.run(); + } catch (UncheckedIOException e) { + IOException cause = e.getCause(); + if (cause == null) throw e; + try { + cause.getClass().asSubclass(exceptionToIgnore); + } catch (ClassCastException f) { + throw e; + } + // Do nothing - OK + } catch (IOException e) { + try { + e.getClass().asSubclass(exceptionToIgnore); + } catch (ClassCastException f) { + throw new UncheckedIOException(e); + } + // Do nothing - OK + } + } + @FunctionalInterface public interface RunnableThrowingIOException { void run() throws IOException; @@ -98,6 +124,29 @@ public class Exceptions { } } + /** Similar to uncheck(), except null is returned if exceptionToIgnore is thrown. */ + public static <R, T extends IOException> R uncheckAndIgnore(SupplierThrowingIOException<R> supplier, Class<T> exceptionToIgnore) { + try { + return supplier.get(); + } catch (UncheckedIOException e) { + IOException cause = e.getCause(); + if (cause == null) throw e; + try { + cause.getClass().asSubclass(exceptionToIgnore); + } catch (ClassCastException f) { + throw e; + } + return null; + } catch (IOException e) { + try { + e.getClass().asSubclass(exceptionToIgnore); + } catch (ClassCastException f) { + throw new UncheckedIOException(e); + } + return null; + } + } + @FunctionalInterface public interface SupplierThrowingIOException<T> { T get() throws IOException; diff --git a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java index 31e27fa2675..390018efdf7 100644 --- a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java +++ b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java @@ -5,8 +5,11 @@ import org.junit.Test; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.file.NoSuchFileException; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; /** * @author bratseth @@ -27,35 +30,38 @@ public class ExceptionsTestCase { @Test public void testUnchecks() { try { - Exceptions.uncheck(this::throwIO); + Exceptions.uncheck(this::throwNoSuchFileException); } catch (UncheckedIOException e) { - assertEquals("root cause", e.getCause().getMessage()); + assertEquals("filename", e.getCause().getMessage()); } try { - Exceptions.uncheck(this::throwIO, "additional %s", "info"); + Exceptions.uncheck(this::throwNoSuchFileException, "additional %s", "info"); } catch (UncheckedIOException e) { assertEquals("additional info", e.getMessage()); } try { - int i = Exceptions.uncheck(this::throwIOWithReturnValue); + int i = Exceptions.uncheck(this::throwNoSuchFileExceptionSupplier); } catch (UncheckedIOException e) { - assertEquals("root cause", e.getCause().getMessage()); + assertEquals("filename", e.getCause().getMessage()); } try { - int i = Exceptions.uncheck(this::throwIOWithReturnValue, "additional %s", "info"); + int i = Exceptions.uncheck(this::throwNoSuchFileExceptionSupplier, "additional %s", "info"); } catch (UncheckedIOException e) { assertEquals("additional info", e.getMessage()); } + + Exceptions.uncheckAndIgnore(this::throwNoSuchFileException, NoSuchFileException.class); + assertNull(Exceptions.uncheckAndIgnore(this::throwNoSuchFileExceptionSupplier, NoSuchFileException.class)); } - private void throwIO() throws IOException { - throw new IOException("root cause"); + private void throwNoSuchFileException() throws IOException { + throw new NoSuchFileException("filename"); } - private int throwIOWithReturnValue() throws IOException { - throw new IOException("root cause"); + private int throwNoSuchFileExceptionSupplier() throws IOException { + throw new NoSuchFileException("filename"); } } |