/** * Retries an action every ms, for 250ms, until it finally works or fails. * * Makes FS operations more reliable. */ private static <T> T retry(File input, Throwing.Function<File, T> function) { long start = System.currentTimeMillis(); Throwable lastException; do { try { return function.apply(input); } catch (Throwable e) { lastException = e; Errors.suppress().run(() -> Thread.sleep(1)); } } while (System.currentTimeMillis() - start < MS_RETRY); throw Errors.asRuntime(lastException); } }
/** * Retries an action every ms, for 250ms, until it finally works or fails. * * Makes FS operations more reliable. */ private static <T> T retry(File input, Throwing.Function<File, T> function) { long start = System.currentTimeMillis(); Throwable lastException; do { try { return function.apply(input); } catch (Throwable e) { lastException = e; Errors.suppress().run(() -> Thread.sleep(1)); } } while (System.currentTimeMillis() - start < MS_RETRY); throw Errors.asRuntime(lastException); }
@Test public void testWrapWithDefault() { // function Assert.assertEquals("called", Errors.suppress().wrapWithDefault(input -> "called", "default").apply(null)); Assert.assertEquals("default", Errors.suppress().wrapWithDefault(input -> { throw new IllegalArgumentException(); }, "default").apply(null)); // supplier Assert.assertEquals("called", Errors.suppress().wrapWithDefault(() -> "called", "default").get()); Assert.assertEquals("default", Errors.suppress().wrapWithDefault(() -> { throw new IllegalArgumentException(); }, "default").get()); // predicate Assert.assertEquals(true, Errors.suppress().wrapWithDefault(input -> true, false).test(null)); Assert.assertEquals(false, Errors.suppress().wrapWithDefault(input -> { throw new IllegalArgumentException(); }, false).test(null)); }
/** Calls javaExec() in a way which is friendly with windows classpath limitations. */ public static ExecResult javaExec(Project project, Action<JavaExecSpec> spec) throws IOException { if (OS.getNative().isWindows()) { Box.Nullable<File> classpathJarBox = Box.Nullable.ofNull(); ExecResult execResult = project.javaexec(execSpec -> { // handle the user spec.execute(execSpec); // create a jar which embeds the classpath File classpathJar = toJarWithClasspath(execSpec.getClasspath()); classpathJar.deleteOnExit(); // set the classpath to be just that one jar execSpec.setClasspath(project.files(classpathJar)); // save the jar so it can be deleted later classpathJarBox.set(classpathJar); }); // delete the jar after the task has finished Errors.suppress().run(() -> FileMisc.forceDelete(classpathJarBox.get())); return execResult; } else { return project.javaexec(spec); } }
foodOnPlate.forEach(Errors.suppress().wrap(this::eat));
@Override public void finalize() { // If we're taking the "default value" route, Errors has you covered Food logged = Errors.log().getWithDefault(() -> cook("spaghetti"), CEREAL); // log to twitter Food suppressed = Errors.suppress().getWithDefault(() -> cook("spaghetti"), CEREAL); // suppress to insecurity buffer // If we're taking the "rethrow RuntimeException" route, then specifying a // default value would be nonsensical, so we don't do it Food rethrow = Errors.rethrow().get(() -> cook("spaghetti")); // I'm stressed, don't judge me Errors.suppress().run(() -> { eat(logged); eat(suppressed); eat(rethrow); }); }
@Test public void testWiresCrossed() { DurianPlugins.resetForTesting(); Errors.resetForTesting(); DurianPlugins.register(Errors.Plugins.Log.class, new TestHandler("Log")); DurianPlugins.register(Errors.Plugins.Dialog.class, new TestHandler("Dialog")); try { Errors.suppress().run(ErrorsTest::throwException); try { Errors.rethrow().run(ErrorsTest::throwException); } catch (RuntimeException e) { // it should pass the RuntimeException unphased Assert.assertNull(e.getCause()); } try { Errors.log().run(ErrorsTest::throwException); } catch (RuntimeException e) { Assert.assertEquals("Log", e.getMessage()); } try { Errors.dialog().run(ErrorsTest::throwException); } catch (RuntimeException e) { Assert.assertEquals("Dialog", e.getMessage()); } } finally { DurianPlugins.resetForTesting(); Errors.resetForTesting(); } }