bineanzhou / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Make ProvisionException public, usable and documented #144

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
We should provide a standard pattern for Guice users to catch and handle 
RuntimeExceptions 
that are thrown by their providers.

The goals:
 - Limit injected Providers to throwing only ProvisionExceptions, so users do not have to catch 
RuntimeException
 - Avoid nesting ProvisionExceptions in one another so that ProvisionException.getCause() 
always returns the users' root problem
 - Make ProvisionException public so that it can be caught

This changes the behaviour of an undocumented API. Previously it was possible 
for a user's 
exception (like UnsupportedOperationException or IllegalStateException) to 
bubble up 
unwrapped to the caller of Provider.get(). Changing this from the arbitrary 
exception type to 
ProvisionException introduces backwards incompatibility.

Original issue reported on code.google.com by limpbizkit on 25 Aug 2007 at 8:12

GoogleCodeExporter commented 9 years ago
I've changed ProvisionException to be public and to avoid nesting itself.

I still need to update the Javadocs for Provider, and find a way to reduce the 
attempted duplication of 
contexts. Currently its possible for a single injection to attempt to rewrapped 
multiple times.

Although the format is flexible, the new exception messages look like this when 
injecting A(B(C(D))):

com.google.inject.ProvisionException: Error injecting constructor
  while locating com.google.inject.ProvisionExceptionTest$D
    for parameter 0 at com.google.inject.ProvisionExceptionTest$C.setD(ProvisionExceptionTest.java:73)
  while locating com.google.inject.ProvisionExceptionTest$C
    for field at com.google.inject.ProvisionExceptionTest$B.c(ProvisionExceptionTest.java:68)
  while locating com.google.inject.ProvisionExceptionTest$B
    for parameter 0 at com.google.inject.ProvisionExceptionTest$A.<init>(ProvisionExceptionTest.java:66)
    at com.google.inject.ConstructorInjector.construct(ConstructorInjector.java:162)
    at com.google.inject.InjectorImpl$ImplicitBinding.get(InjectorImpl.java:1040)
    at com.google.inject.InjectorImpl$SingleParameterInjector.inject(InjectorImpl.java:661)
    at com.google.inject.InjectorImpl.getParameters(InjectorImpl.java:692)
    at com.google.inject.InjectorImpl$SingleMethodInjector.inject(InjectorImpl.java:577)
    at com.google.inject.ConstructorInjector.construct(ConstructorInjector.java:155)
    at com.google.inject.InjectorImpl$ImplicitBinding.get(InjectorImpl.java:1040)
    at com.google.inject.InjectorImpl$SingleFieldInjector.inject(InjectorImpl.java:471)
    at com.google.inject.ConstructorInjector.construct(ConstructorInjector.java:155)
    at com.google.inject.InjectorImpl$ImplicitBinding.get(InjectorImpl.java:1040)
    at com.google.inject.InjectorImpl$SingleParameterInjector.inject(InjectorImpl.java:661)
    at com.google.inject.InjectorImpl.getParameters(InjectorImpl.java:692)
    at com.google.inject.ConstructorInjector.construct(ConstructorInjector.java:140)
    at com.google.inject.InjectorImpl$ImplicitBinding.get(InjectorImpl.java:1040)
    at com.google.inject.InjectorImpl$9$1.call(InjectorImpl.java:739)
    at com.google.inject.InjectorImpl.callInContext(InjectorImpl.java:778)
    at com.google.inject.InjectorImpl$9.get(InjectorImpl.java:732)
    at com.google.inject.InjectorImpl.getInstance(InjectorImpl.java:759)
    at com.google.inject.ProvisionExceptionTest.testExceptionsCollapsed(ProvisionExceptionTest.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.textui.TestRunner.doRun(TestRunner.java:116)
    at com.intellij.rt.execution.junit.IdeaTestRunner.doRun(IdeaTestRunner.java:69)
    at junit.textui.TestRunner.doRun(TestRunner.java:109)
    at com.intellij.rt.execution.junit.IdeaTestRunner.startRunnerWithArgs(IdeaTestRunner.java:24)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:118)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
Caused by: java.lang.UnsupportedOperationException
    at com.google.inject.ProvisionExceptionTest$D.<init>(ProvisionExceptionTest.java:77)
    at 
com.google.inject.ProvisionExceptionTest$D$$FastClassByGuice$$5d1f9381.newInstan
ce(<generated>)
    at net.sf.cglib.reflect.FastConstructor.newInstance(FastConstructor.java:40)
    at 
com.google.inject.DefaultConstructionProxyFactory$2.newInstance(DefaultConstruct
ionProxyFactory.java:67)
    at com.google.inject.ConstructorInjector.construct(ConstructorInjector.java:142)
    ... 34 more

Original comment by limpbizkit on 25 Aug 2007 at 8:15

GoogleCodeExporter commented 9 years ago
Note that I am unwrapping InvocationTargetExceptions. So if the user's 
Constructor looks like this:
   public MyClass() {
      throw new MyException();
   }
then the cause of the thrown ProvisionException will be a MyException and not 
an InvocationTargetException.

This hides the user from reflection in the current implementation, and it makes 
the catch() block more 
understandable.

Original comment by limpbizkit on 25 Aug 2007 at 8:19

GoogleCodeExporter commented 9 years ago
aren't issue 23 and issue 111 related?

Original comment by robbie.v...@gmail.com on 25 Aug 2007 at 10:47

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 8 Jun 2008 at 11:32

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 2 Nov 2008 at 5:22