})); } catch (CreationException ex) { ex.getErrorMessages().stream().forEach(m -> { if (m.getCause() != null) { LOGGER.error(m.getCause().getMessage()); } else { LOGGER.error(m.getMessage());
public void throwCreationExceptionIfErrorsExist() { if (!hasErrors()) { return; } throw new CreationException(getMessages()); }
/** Creates a CreationException containing {@code messages}. */ public CreationException(Collection<Message> messages) { this.messages = ImmutableSet.copyOf(messages); checkArgument(!this.messages.isEmpty()); initCause(Messages.getOnlyCause(this.messages)); }
public void testNoMatchingConstrucotsBecauseTooLittleParams() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder().build(NullFactory.class)); } }); fail("should have failed"); } catch (CreationException expected) { Asserts.assertContains( expected.getMessage(), "1) " + Foo.class.getName() + " has @AssistedInject constructors, but none of them match the parameters in method " + NullFactory.class.getName()); } }
public void testImplicitForwardingAssistedBindingFailsWithAbstractClass() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(AbstractCar.class).to(ArtCar.class); install(new FactoryModuleBuilder().build(ColoredAbstractCarFactory.class)); } }); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "1) " + AbstractCar.class.getName() + " is abstract, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + AbstractCar.class.getName(), "at " + ColoredAbstractCarFactory.class.getName() + ".create("); assertEquals(1, ce.getErrorMessages().size()); } }
@Test public void testInvalidLogPatternBadSpecifier() { final Config config = Config.getFixedConfig("ness.httpserver.request-log.syslog.enabled", "true", "ness.httpserver.request-log.syslog.fields", "remoteAddr,foo"); try { final Injector inj = Guice.createInjector(Stage.PRODUCTION, disableStuff(), new LogFieldsModule(), new SyslogRequestLogModule(config)); inj.injectMembers(this); fail(); } catch (CreationException ce) { Assert.assertEquals(IllegalArgumentException.class, ce.getCause().getClass()); Assert.assertEquals("Invalid log pattern: unknown field <foo>", ce.getCause().getMessage()); } }
public void testGenericErrorMessageMakesSense() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder().build(Key.get(Foo.Factory.class))); } }); fail(); } catch (CreationException ce) { // Assert not only that it's the correct message, but also that it's the *only* message. Collection<Message> messages = ce.getErrorMessages(); assertEquals( Foo.Factory.class.getName() + " cannot be used as a key; It is not fully specified.", Iterables.getOnlyElement(messages).getMessage()); } }
Injector optionsInjector = Guice.createInjector(new OptionsModule(args)); CaliperOptions options = optionsInjector.getInstance(CaliperOptions.class); Module runnerModule = new ExperimentingRunnerModule(); propogateIfCaliperException(e.getCause()); throw e; } catch (ProvisionException e) { propogateIfCaliperException(e.getCause()); for (Message message : e.getErrorMessages()) { propogateIfCaliperException(message.getCause());
public void testServletModule_badPattern() { try { Guice.createInjector( new ServletModule() { @Override protected void configureServlets() { serve("/%2E/*").with(new DummyServlet()); serveRegex("/(foo|bar/").with(new DummyServlet()); filter("/%2E/*").through(new DummyFilterImpl()); filterRegex("/(foo|bar/").through(new DummyFilterImpl()); } }); fail(); } catch (CreationException e) { assertEquals(4, e.getErrorMessages().size()); } }
private Properties readPropertyFile(String propertyFile) { Properties properties = new Properties(); InputStream is = null; try { is = ResourceLoader.openResource(propertyFile); properties.load(is); } catch (IOException e) { throw new CreationException(Arrays.asList( new Message("Unable to load properties: " + propertyFile))); } finally { IOUtils.closeQuietly( is ); } return properties; }
@VisibleForTesting static <T extends Throwable> T getFirstThrowableOfType(CreationException e, Class<T> clazz) { for (Message message : e.getErrorMessages()) { if (message.getCause() != null) { T cause = getFirstThrowableOfType(message.getCause(), clazz); if (cause instanceof ProvisionException) return getFirstThrowableOfType(ProvisionException.class.cast(cause), clazz); else if (cause instanceof TransformParallelException) return getFirstThrowableOfType(TransformParallelException.class.cast(cause), clazz); else if (cause instanceof CreationException) return getFirstThrowableOfType(CreationException.class.cast(cause), clazz); return cause; } } return null; }
"Could not create an instance of the benchmark class following reasons:"); int errorNum = 0; for (Message guiceMessage : e.getErrorMessages()) { message.append("\n ").append(++errorNum).append(") ") .append(guiceMessage.getMessage());
protected void annotateInjectorCreationException(CreationException e) { annotateInjectorExceptions(e.getErrorMessages()); throw e; }
/** * Main entry point to the application * * @param args CLI arguments * @throws Exception */ public static void main(String[] args) throws Exception { try { new MultiBitExchangeApiWebService(args).run(args); } catch (CreationException e) { System.err.printf("*********%nException on start up - is 'mongod' running and accessible?%n*********%n"); System.err.printf(String.format("%s:\n%s", e.getClass().getName(), e.getMessage())); } }
o = processExceptions(ce.getCause(), ce); } catch (ProvisionException pe) {
public void testFactoryFailsWithMissingBinding() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(ColoredCarFactory.class) .toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class)); } }); fail(); } catch (CreationException expected) { assertContains( expected.getMessage(), "Could not find a suitable constructor in java.lang.Double.", "at " + ColoredCarFactory.class.getName() + ".create(FactoryProvider2Test.java"); } }
public void testImplicitForwardingAssistedBindingFailsWithInterface() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(Car.class).to(Golf.class); install(new FactoryModuleBuilder().build(ColoredCarFactory.class)); } }); fail(); } catch (CreationException ce) { assertContains( ce.getMessage(), "1) " + Car.class.getName() + " is an interface, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create("); assertEquals(1, ce.getErrorMessages().size()); } }
@Test public void testInvalidLogPatternTooManyColons() { try { final Config config = Config.getFixedConfig("ness.httpserver.request-log.file.enabled", "true", "ness.httpserver.request-log.file.fields", "remoteAddr,cookie:no-here:bar"); final Injector inj = Guice.createInjector(Stage.PRODUCTION, disableStuff(), new LogFieldsModule(), new FileRequestLogModule(config)); inj.injectMembers(this); fail(); } catch (CreationException ce) { Assert.assertEquals(IllegalArgumentException.class, ce.getCause().getClass()); Assert.assertEquals("Bad specifier \"cookie:no-here:bar\" has too many colons", ce.getCause().getMessage()); } }
public void testFactoryPublicAndReturnTypeNotPublic() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { install( new FactoryModuleBuilder() .implement(Hidden.class, HiddenImpl.class) .build(NotHidden.class)); } }); fail("Expected CreationException"); } catch (CreationException ce) { assertEquals( NotHidden.class.getName() + " is public, but has a method that returns a non-public type: " + Hidden.class.getName() + ". Due to limitations with java.lang.reflect.Proxy, this is not allowed. " + "Please either make the factory non-public or the return type public.", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage()); } }
Injector optionsInjector = Guice.createInjector(new OptionsModule(args)); CaliperOptions options = optionsInjector.getInstance(CaliperOptions.class); Module runnerModule = new ExperimentingRunnerModule(); propogateIfCaliperException(e.getCause()); throw e; } catch (ProvisionException e) { propogateIfCaliperException(e.getCause()); for (Message message : e.getErrorMessages()) { propogateIfCaliperException(message.getCause());