xiaodududu / google-guice

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

NPE in ProvisionException #540

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
How to reproduce:

throw new ProvisionException(null, new RuntimeException());

Exception:

Exception in thread "main" java.lang.NullPointerException: message
    at com.google.inject.internal.util.Preconditions.checkNotNull(Preconditions.java:200)
    at com.google.inject.spi.Message.<init>(Message.java:52)
    at com.google.inject.ProvisionException.<init>(ProvisionException.java:46)

Use case: 
When working on mycila-guice (Guice's extensions), I need to sometimes rethrow 
the targe texception of a reflection call like below. But the message can be 
null. The ProvisionException crashes. 

Thus, we are forced to prevent it by doing a String.valueOf(...)

try {
...
} catch (InvocationTargetException e) {
    throw new ProvisionException(String.valueOf(e.getTargetException().getMessage()), e.getTargetException());
}

Original issue reported on code.google.com by mathieu....@gmail.com on 23 Sep 2010 at 3:49

GoogleCodeExporter commented 9 years ago
Note: we are using a snapshot build of the trunk of mid-september 2010.

Original comment by mathieu....@gmail.com on 23 Sep 2010 at 3:50

GoogleCodeExporter commented 9 years ago
Why do you need to wrap it in a ProvisionException?  If the target is a runtime 
exception or error, can you just rethrow it directly?  And if it's a checked 
exception, is there a reasonable error message you can provide that's not a 
duplicate of the exception message?  Something like, "Error calling <some user 
method>"?

Original comment by sberlin on 24 Sep 2010 at 2:34

GoogleCodeExporter commented 9 years ago
Yes you're right i could do that i think. I've used the Guice's provision 
exception because it's an injection listener which inject resource bundles, 
nearly like Google Sitebrick does or Guiceyfruit does for the JSR250 support. 

Since its injection stuff, I think a provision exception is a good business 
exception which can be thrown when an exception occurs in injection.

Isn't the case ?

Original comment by mathieu....@gmail.com on 24 Sep 2010 at 12:24

GoogleCodeExporter commented 9 years ago
I think Guice should take care of wrapping any user exceptions into a 
ProvisionException before it hits user code.  (You should check on this, 
though.)

Original comment by sberlin on 24 Sep 2010 at 12:56

GoogleCodeExporter commented 9 years ago
There is a related issue on the guicey-fruit issue tracker, which are 
struggling with the same issue : 
http://code.google.com/p/guiceyfruit/issues/detail?id=27

Original comment by timo.wes...@gmail.com on 12 Nov 2010 at 1:10