@Override public void run() { try { job.apply(listener); } catch (RuntimeException e) { log.error("Problem notifying listener "+listener+" of "+job, e); Exceptions.propagateIfFatal(e); } finally { listenerQueueSize.decrementAndGet(); } }}); }
/** * If machine is restarting, then will get WinRM IOExceptions */ protected Throwable findExceptionCausedByWindowsRestart(Exception e) { return Exceptions.getFirstThrowableOfType(e, WebServiceException.class) != null ? Exceptions.getFirstThrowableOfType(e, WebServiceException.class/*Wraps Soap exceptions*/) : Exceptions.getFirstThrowableOfType(e, Fault.class/*Wraps IO exceptions*/); }
public void handleException(Throwable throwable) throws Exception { Exceptions.propagateIfFatal(throwable); if (throwable instanceof Exception) { // allow checked exceptions to be passed through throw (Exception)throwable; } throw Exceptions.propagate(throwable); }
private <T> T constructWithSpecial(Class<? extends SpecialBrooklynObjectConstructor> special, Class<? extends T> clazz, AbstractBrooklynObjectSpec<?, ?> spec) { try { return special.newInstance().create(managementContext, clazz, spec); } catch (Exception e) { Exceptions.propagateIfFatal(e); throw Exceptions.propagateAnnotated("Unable to create "+clazz+" "+spec+" using special "+special, e); } }
@Override public void run() { try { assertPortsAvailable(ports); } catch (Throwable t) { if (!logged) { LOG.warn("Port(s) not available; waiting for up to "+timeout+" ("+Exceptions.getFirstInteresting(t)+")"); logged = true; } throw Exceptions.propagate(t); } }}); LOG.debug("Ports are available: "+ports);
@Test public void testPropagateWithoutAnnotationSuppressed() throws Exception { Throwable t = new Throwable("test"); try { Exceptions.propagate(t); } catch (Throwable t2) { t = t2; } Assert.assertEquals(Exceptions.collapseText(t), "test"); }
@Test(dataProvider = "brooklynObjects") public void testCannotUpdateConfigToInvalidValue(BrooklynObject object) { try { object.config().set(EntityRequiringConfigKeyInRange.RANGE, -1); fail("Expected exception when calling config().set with invalid value on " + object); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
/** like {@link #collapse(Throwable)} but returning a one-line message suitable for logging without traces */ public static String collapseText(Throwable t) { return collapseText(t, false); }
@Test public void testPropagateWithAnnotationExplicitNotSuppressed() throws Exception { Throwable t = new RuntimeException("test"); try { Exceptions.propagateAnnotated("important", t); } catch (Throwable t2) { t = t2; } Assert.assertEquals(Exceptions.collapseText(t), "important: test"); }
@Override public String toString() { if (causeEmbeddedInMessage) { return super.toString(); } else { return Exceptions.appendSeparator(super.toString(), Exceptions.collapseText(getCause())); } }
@Test public void testPropagateWithAnnotationNotExplicitIncludedWhenWrapped() throws Exception { Throwable t = new Throwable("test"); try { Exceptions.propagateAnnotateIfWrapping("important", t); } catch (Throwable t2) { t = t2; } Assert.assertEquals(Exceptions.collapseText(t), "important: test"); }
/** Implements the return behavior for {@link #expectedFailureOfType(Throwable, Class...)} and others, * to log interesting earlier errors but to suppress those which are internal or redundant. */ private static void rethrowPreferredException(Throwable earlierPreferredIfFatalElseLogged, Throwable laterPreferredOtherwise) throws AssertionError { if (!(earlierPreferredIfFatalElseLogged instanceof AssertionError)) { Exceptions.propagateIfFatal(earlierPreferredIfFatalElseLogged); } log.warn("Detail of unexpected error: "+earlierPreferredIfFatalElseLogged, earlierPreferredIfFatalElseLogged); throw Exceptions.propagate(laterPreferredOtherwise); }
@Test public void testPropagateNoMessageGivesType() throws Exception { Throwable t = new Throwable(); Assert.assertEquals(Exceptions.collapseText(t), Throwable.class.getName()); try { Exceptions.propagate(t); } catch (Throwable t2) { t = t2; } Assert.assertEquals(Exceptions.collapseText(t), Throwable.class.getName()); }
@Test public void testExceptionWhenSubclassSetsInvalidDefaultValue() { try { app.createAndManageChild(EntitySpec.create(EntityProvidingDefaultValueForConfigKeyInRange.class)); fail("Expected exception when managing entity setting invalid default value"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
/** as {@link #collapseText(Throwable)} but skipping any throwables which implement {@link CanSkipInContext} * and indicate they should be skipped any of the given contexts */ public static String collapseTextInContext(Throwable t, Object ...contexts) { return collapseText(t, false, ImmutableSet.<Throwable>of(), contexts); }
private static URL[] asUrls(String[] urls) { URL[] urlo = new URL[urls.length]; try { for (int i=0; i<urls.length; i++) urlo[i] = new URL(urls[i]); } catch (MalformedURLException e) { throw Exceptions.propagate(e); } return urlo; }
/** Same as {@link #destroy(Location)} but catching all errors. */ public static void destroyCatching(Location loc) { try { destroy(loc); } catch (Exception e) { log.warn("ERROR destroying "+loc+" (ignoring): "+e, e); Exceptions.propagateIfFatal(e); } }
protected void handleSubsystemStartupError(boolean ignoreSuchErrors, String system, Exception e) { Exceptions.propagateIfFatal(e); if (ignoreSuchErrors) { LOG.error("Subsystem for "+system+" had startup error (continuing with startup): "+e, e); if (managementContext!=null) ((ManagementContextInternal)managementContext).errors().add(e); } else { throw Exceptions.propagate(e); } }
@Override public void update() { try { Task<?> task = updateAsync(); if (task != null) task.getUnchecked(); ServiceStateLogic.ServiceProblemsLogic.clearProblemsIndicator(this, "update"); } catch (Exception e) { ServiceStateLogic.ServiceProblemsLogic.updateProblemsIndicator(this, "update", "update failed with: "+Exceptions.collapseText(e)); throw Exceptions.propagate(e); } }