private void validateNullableFails(Injector injector, Module module) { try { injector.getInstance(Integer.class); fail(); } catch (ProvisionException expected) { assertContains( expected.getMessage(), "1) null returned by binding at " + module.getClass().getName() + ".configure(", "but the 1st parameter of " + module.getClass().getName() + ".fail(", "is not @Nullable", "while locating java.lang.String", "for the 1st parameter of " + module.getClass().getName() + ".fail(", "while locating java.lang.Integer"); assertEquals(1, expected.getErrorMessages().size()); } }
public void testScopedProviderMethodThrowsException() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() {} @Provides @Singleton int provideInt() { throw new RuntimeException("boom"); } }); Provider<Integer> intProvider = injector.getProvider(Integer.class); try { intProvider.get(); fail(); } catch (ProvisionException pe) { // by default assertContains asserts that the last item doesn't repeat... which is the main // thing we are testing for assertContains(pe.getMessage(), "java.lang.RuntimeException: boom", "provideInt"); } }
@Override public void close() { if (isClosed()) { throw new ClosedInjectorException(this); } for (Key<?> key : delegate.getAllBindings().keySet()) { try { com.google.inject.Binding<?> binding = delegate.getExistingBinding(key); if (!Scopes.isSingleton(binding)) { continue; } invokeAnnotatedMethod(binding.getProvider().get(), PreDestroy.class); } catch (ProvisionException pe) { if (!(pe.getCause() instanceof TypeNotFoundException)) { pe.printStackTrace(); } } } synchronized (lock) { closed = true; } }
public static void prettyPrintExceptions(Throwable t, PrintStream out) { if (t instanceof $ComputationException) { getRootCause(t).printStackTrace(out); } else if (t instanceof CreationException) { ((CreationException) t) .getErrorMessages() .forEach( error -> { // out.println(error.getSources()); // out.println(error.getMessage()); // out.println(); if (error.getCause() != null) { error.getCause().printStackTrace(); } else { System.err.println(error.getMessage()); System.err.println("when injecting : " + error.getSources()); System.err.println(); } }); } else if (t instanceof ProvisionException) { ((ProvisionException) t).getErrorMessages().forEach(msg -> msg.getCause().printStackTrace()); } }
protected void annotateProvisionException(ProvisionException e) { annotateInjectorExceptions(e.getErrorMessages()); throw e; }