/** Iterates over all XkcdColor entries to find the closest color by brute-force. */ private Map.Entry<String, RGB> closestTo(RGB rgb) { Errors.log().run(() -> Thread.sleep(10)); return colors.all().stream() .map(entry -> Maps.immutableEntry(distance(entry.getValue(), rgb), entry)) .min(Comparator.comparing(Map.Entry::getKey)) .get().getValue(); }
private void deleteStragglers(File root, Set<File> toKeep, String... dirs) { for (String dir : dirs) { File dirRoot = new File(root, dir); if (dirRoot.exists()) { for (File file : FileMisc.list(dirRoot)) { if (!toKeep.contains(file)) { Errors.log().run(() -> { FileMisc.forceDelete(file); }); } } } } }
@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(); } }
/** 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); } }
@Test(expected = AssertionError.class) public void testAssertionPlugin() { DurianPlugins.resetForTesting(); Errors.resetForTesting(); try { // set the Log handler to the be the OnErrorThrowAssertion System.setProperty("durian.plugins.com.diffplug.common.base.Errors.Plugins.Log", "com.diffplug.common.base.Errors$Plugins$OnErrorThrowAssertion"); // send something to the suppress handler, but we should get an AssertionError Errors.log().run(() -> { throw new RuntimeException("Didn't see this coming."); }); } finally { System.clearProperty("durian.plugins.com.diffplug.common.base.Errors.Plugins.Suppress"); DurianPlugins.resetForTesting(); Errors.resetForTesting(); } }
/** * 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); }
@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); }); }