/** 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); }
public Constructor<?> loadConstructor(Class<?> clazz, Class<?>[] argTypes) throws ReflectionAccessException { try { return clazz.getConstructor(argTypes); } catch (SecurityException e) { throw new ReflectionAccessException("Failed to load constructor of class '" + clazz + " with argument types " + Arrays.asList(argTypes) + ": " + Exceptions.collapseText(e), e); } catch (NoSuchMethodException e) { throw new ReflectionAccessException("Failed to load constructor of class '" + clazz + " with argument types " + Arrays.asList(argTypes) + ": " + Exceptions.collapseText(e), e); } }
public Method loadMethod(Class<?> clazz, String methodName, Class<?>[] argTypes) throws ReflectionNotFoundException, ReflectionAccessException { try { return clazz.getMethod(methodName, argTypes); } catch (NoClassDefFoundError e) { throw new ReflectionNotFoundException("Failed to invoke method " + methodName + " on class " + clazz + " with argument types " + Arrays.asList(argTypes) + ", using class loader " + clazz.getClassLoader() + ": " + Exceptions.collapseText(e), e); } catch (NoSuchMethodException e) { throw new ReflectionNotFoundException("Failed to invoke method " + methodName + " on class " + clazz + " with argument types " + Arrays.asList(argTypes) + ": " + Exceptions.collapseText(e), e); } catch (SecurityException e) { throw new ReflectionAccessException("Failed to invoke method " + methodName + " on class " + clazz + " with argument types " + Arrays.asList(argTypes) + ": " + Exceptions.collapseText(e), e); } }
public Object invokeStaticMethod(Method method, Object... argValues) throws IllegalArgumentException, ReflectionAccessException { try { return method.invoke(null, argValues); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(getIllegalArgumentsErrorMessage(method, argValues), e); } catch (IllegalAccessException e) { throw new ReflectionAccessException("Failed to invoke method '" + method.toGenericString() + " on class " + method.getDeclaringClass() + " with argument values " + Arrays.asList(argValues) + ": " + Exceptions.collapseText(e), e); } catch (InvocationTargetException e) { throw new ReflectionAccessException("Failed to invoke method '" + method.toGenericString() + " on class " + method.getDeclaringClass() + " with argument values " + Arrays.asList(argValues) + ": " + Exceptions.collapseText(e), e); } }
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */ public Object loadInstance(String classname) throws ReflectionNotFoundException, ReflectionAccessException { Class<?> clazz = loadClass(classname); try { return clazz.newInstance(); } catch (InstantiationException e) { throw new ReflectionAccessException("Failed to create instance of class '" + classname + "' using class loader " + classLoader + ": " + Exceptions.collapseText(e), e); } catch (IllegalAccessException e) { throw new ReflectionAccessException("Failed to create instance of class '" + classname + "' using class loader " + classLoader + ": " + Exceptions.collapseText(e), e); } }
@Override public String toString() { if (causeEmbeddedInMessage) { return super.toString(); } else { return Exceptions.appendSeparator(super.toString(), Exceptions.collapseText(getCause())); } }
private static RuntimeException propagate(Entity entity, Effector<?> effector, Throwable throwable) { if (throwable instanceof EffectorCallPropagatedRuntimeException && Objects.equal(makeMessage(entity, effector), throwable.getMessage())) { // Avoid throwing a PropagatedRuntimeException that just repeats the last PropagatedRuntimeException. throw (EffectorCallPropagatedRuntimeException)throwable; } EffectorCallPropagatedRuntimeException result = new EffectorCallPropagatedRuntimeException(entity, effector, throwable); log.warn(Exceptions.collapseText(result)); log.debug(makeMessage(entity, effector), throwable); throw result; } }
@Test public void testCollapseIncludesRootCause() throws Exception { NullPointerException npe = new NullPointerException("simulated2"); IllegalStateException ise = new IllegalStateException("simulated1", npe); assertEquals(Exceptions.collapseText(ise), "simulated1: NullPointerException: simulated2"); }
@Test public void testExec() { Throwable t; t = new IOException("1"); t = new java.util.concurrent.ExecutionException(t); Assert.assertEquals(Exceptions.collapseText(t), "IOException: 1"); }
@Test public void testCollapseTextWhenExceptionMessageEmpty() throws Exception { String text = Exceptions.collapseText(new ExecutionException(new IllegalStateException())); Assert.assertNotNull(text); }
@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"); }
@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"); }
@Test public void testNestedExecAndProp() { Throwable t; t = new IOException("1"); t = new org.apache.brooklyn.util.exceptions.PropagatedRuntimeException(t); t = new java.util.concurrent.ExecutionException(t); Assert.assertEquals(Exceptions.collapseText(t), "IOException: 1"); }
@Test public void testNestedPropWithMessage() { Throwable t; t = new IOException("1"); t = new org.apache.brooklyn.util.exceptions.PropagatedRuntimeException(t); t = new org.apache.brooklyn.util.exceptions.PropagatedRuntimeException("A", t); Assert.assertEquals(Exceptions.collapseText(t), "A: IOException: 1"); }
@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)); } }
@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); } }
@Test public void testExceptionWhenValueSetByName() { try { app.createAndManageChild(EntitySpec.create(EntityRequiringConfigKeyInRange.class) .configure(ImmutableMap.of("test.conf.range", -1))); fail("Expected exception when managing entity with invalid config"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
@Test public void testExceptionIsThrownWhenUserNullsConfigWithNonNullDefault() { try { app.createAndManageChild(EntitySpec.create(EntityWithNonNullConstraintWithNonNullDefault.class) .configure(EntityWithNonNullConstraintWithNonNullDefault.NON_NULL_WITH_DEFAULT, (Object) null)); fail("Expected exception when config key set to null"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
@Test public void testExceptionWhenPolicyHasInvalidConfig() { try { mgmt.getEntityManager().createPolicy(PolicySpec.create(PolicyWithConfigConstraint.class) .configure(PolicyWithConfigConstraint.NON_NULL_CONFIG, (Object) null)); fail("Expected exception when creating policy with missing config"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }
@Test(enabled = false) public void testExceptionWhenPolicyHasNullForeignConfig() { Policy p = mgmt.getEntityManager().createPolicy(PolicySpec.create(TestPolicy.class) .configure(EntityWithNonNullConstraint.NON_NULL_CONFIG, (Object) null)); try { ConfigConstraints.assertValid(p); fail("Expected exception when validating policy with missing config"); } catch (Exception e) { Throwable t = Exceptions.getFirstThrowableOfType(e, ConstraintViolationException.class); assertNotNull(t, "Original exception was: " + Exceptions.collapseText(e)); } }