xiaodududu / google-guice

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

provider doesn't seem to "remember" exceptions #483

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Here's a hack I've had to put in to deal with the fact that the same singleton 
provider is called 
multiple times even when it throws an exception.  This is against 2.1 (r1128)

   private RuntimeException regionException = null;

   @Provides
   @Singleton
   @EC2
   Map<String, URI> provideRegions(AvailabilityZoneAndRegionClient client) {
      // guice doesn't remember when singleton providers throw exceptions.
      // in this case, if describeRegions fails, it is called again for
      // each provider method that depends on it. To short-circuit this,
      // we remember the last exception trusting that guice is single-threaded
      if (regionException != null)
         throw regionException;
      try {
         return client.describeRegions();
      } catch (RuntimeException e) {
         this.regionException = e;
         throw e;
      }
   }

Original issue reported on code.google.com by adrian.f...@gmail.com on 21 May 2010 at 9:29

GoogleCodeExporter commented 9 years ago
If you would like your providers to throw & allow them to be scoped, consider 
using the ThrowingProviders extension.  As of r1312, it now supports a nice 
shiny @ThrowingProvides annotation for throwing provider methods (similar to 
@Provides).

Original comment by sberlin on 25 Oct 2010 at 4:11