Confusing error message if exception is thrown from `configure`
- Dominant language
- Java
- Stars
- 12.7k
- Forks
- 1.7k
- Avg merge
- 11m
- Merged PRs (30d)
- 2
Description
If an exception is thrown during an execution of `configure`, you can get some very confusing error messages. For example, consider the following code.
```java
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Provides;
import jakarta.inject.Singleton;
import java.util.concurrent.ThreadLocalRandom;
class MyClass {
interface MyInterface {
}
static class MyImplementation implements MyInterface {
}
static class MyModule extends AbstractModule {
@Override
protected void configure() {
if (ThreadLocalRandom.current().nextBoolean()) {
throw new RuntimeException("Exception thrown!");
}
bind(MyInterface.class).toInstance(new MyImplementation());
}
@Provides
@Singleton
String provideString(MyInterface myInterface) {
return "Result";
}
}
public static void main(String[] args) {
Guice.createInjector(new MyModule());
}
}
```
If the `RuntimeException` is not thrown then the injector is created without issue.
On the other hand, if the `RuntimeException` is thrown you get a long error message including the following:
```
Exception in thread "main" com.google.inject.CreationException: Unable to create injector, see the following errors:
1) [Guice/MissingImplementation]: No implementation for MyClass$MyInterface was bound.
Requested by:
1 : MyClass$MyModule.provideString(MyClass.java:43)
\_ for 1st parameter myInterface
at MyClass$MyModule.provideString(MyClass.java:43)
Learn more:
https://github.com/google/guice/wiki/MISSING_IMPLEMENTATION
2) An exception was caught and reported. Message: Exception thrown!
at [unknown source]
```
Reporting that no implementation of `MyClass$MyInterface` is bound is not helpful. In fact what happened is that an exception was thrown from `configure` before the line creating the binding was reached.
In situations like this where the invocation of `configure` throws an exception, it probably doesn't make sense to ever report in addition that there are missing bindings.
I am using Guice version `7.0.0`.
Contributor guide
Assessment
This issue has not been assessed yet.