Skip to content

Commit

Permalink
Rewrote the "child binding already set" error message for better clar…
Browse files Browse the repository at this point in the history
…ity.

PiperOrigin-RevId: 698811431
  • Loading branch information
sameb authored and Guice Team committed Nov 21, 2024
1 parent 21381e3 commit bf43b4f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ final class ChildBindingAlreadySetError extends InternalErrorDetail<ChildBinding
super(
ErrorId.CHILD_BINDING_ALREADY_SET,
String.format(
"Unable to create binding for %s because it was already configured on one or more"
+ " child injectors or private modules.",
"A binding for %s already exists in one or more child injectors or private modules."
+ " Bindings from children can only be used by a parent if the child was a"
+ " PrivateModule and the binding was exposed. Parent injectors cannot create"
+ " bindings that already exist in a child.",
Messages.convert(key)),
sources,
null);
Expand Down
8 changes: 5 additions & 3 deletions core/test/com/google/inject/JitBindingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ private String jitInParentFailed(TypeLiteral<?> clazz) {
}

private String inChildMessage(Class<?> clazz) {
return "Unable to create binding for "
+ "JitBindingsTest$"
return "A binding for JitBindingsTest$"
+ clazz.getSimpleName()
+ " because it was already configured on one or more child injectors or private modules";
+ " already exists in one or more child"
+ " injectors or private modules. Bindings from children can only be used by a parent if"
+ " the child was a PrivateModule and the binding was exposed. Parent injectors cannot"
+ " create bindings that already exist in a child.";
}

public void testLinkedBindingWorks() {
Expand Down
10 changes: 7 additions & 3 deletions core/test/com/google/inject/ParentInjectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** @author [email protected] (Jesse Wilson) */
/**
* @author [email protected] (Jesse Wilson)
*/
@RunWith(JUnit4.class)
public class ParentInjectorTest {

Expand Down Expand Up @@ -70,8 +72,10 @@ public void testParentJitBindingWontClobberChildBinding() {
} catch (ConfigurationException e) {
assertContains(
e.getMessage(),
"Unable to create binding for ParentInjectorTest$A because it was already configured on"
+ " one or more child injectors or private modules.",
"A binding for ParentInjectorTest$A already exists in one or more child injectors or"
+ " private modules. Bindings from children can only be used by a parent if the child"
+ " was a PrivateModule and the binding was exposed. Parent injectors cannot create"
+ " bindings that already exist in a child.",
"ParentInjectorTest$9.configure");
}
}
Expand Down
22 changes: 15 additions & 7 deletions core/test/com/google/inject/PrivateModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import java.util.List;
import junit.framework.TestCase;

/** @author [email protected] (Jesse Wilson) */
/**
* @author [email protected] (Jesse Wilson)
*/
public class PrivateModuleTest extends TestCase {

public void testBasicUsage() {
Expand Down Expand Up @@ -589,8 +591,10 @@ public void testParentBindsSomethingInPrivate() {
assertEquals(1, expected.getErrorMessages().size());
assertContains(
expected.toString(),
"Unable to create binding for List<String> ",
"because it was already configured on one or more child injectors or private modules.",
"A binding for List<String> already exists in one or more child injectors",
" or private modules. Bindings from children can only be used by a parent if the child",
" was a PrivateModule and the binding was exposed. Parent injectors cannot create",
" bindings that already exist in a child.",
"1 : PrivateModuleTest$FailingPrivateModule.configure",
"PrivateModuleTest$FailingModule -> PrivateModuleTest$ManyPrivateModules ->"
+ " PrivateModuleTest$FailingPrivateModule",
Expand All @@ -610,8 +614,10 @@ public void testParentBindingToPrivateLinkedJitBinding() {
assertEquals(1, expected.getErrorMessages().size());
assertContains(
expected.toString(),
"Unable to create binding for List<String> because it was already configured on one or"
+ " more child injectors or private modules",
"A binding for List<String> already exists in one or more child injectors",
" or private modules. Bindings from children can only be used by a parent if the child",
" was a PrivateModule and the binding was exposed. Parent injectors cannot create",
" bindings that already exist in a child.",
"1 : PrivateModuleTest$FailingPrivateModule.configure",
"PrivateModuleTest$ManyPrivateModules -> PrivateModuleTest$FailingPrivateModule",
"2 : PrivateModuleTest$SecondFailingPrivateModule.configure",
Expand All @@ -628,8 +634,10 @@ public void testParentBindingToPrivateJitBinding() {
assertEquals(1, expected.getErrorMessages().size());
assertContains(
expected.toString(),
"Unable to create binding for PrivateModuleTest$PrivateFoo because it was already"
+ " configured on one or more child injectors or private modules.",
"A binding for PrivateModuleTest$PrivateFoo already exists in one or more child",
" injectors or private modules. Bindings from children can only be used by a parent if",
" the child was a PrivateModule and the binding was exposed. Parent injectors cannot",
" create bindings that already exist in a child.",
"as a just-in-time binding");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guice configuration errors:

1) [Guice/ChildBindingAlreadySet]: Unable to create binding for ChildBindingAlreadySetErrorTest$DependsOnFoo because it was already configured on one or more child injectors or private modules.
1) [Guice/ChildBindingAlreadySet]: A binding for ChildBindingAlreadySetErrorTest$DependsOnFoo already exists in one or more child injectors or private modules. Bindings from children can only be used by a parent if the child was a PrivateModule and the binding was exposed. Parent injectors cannot create bindings that already exist in a child.

Bound at:
1 : ChildBindingAlreadySetErrorTest$ChildModule4.configure(ChildBindingAlreadySetErrorTest.java:105)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guice configuration errors:

1) [Guice/ChildBindingAlreadySet]: Unable to create binding for ChildBindingAlreadySetErrorTest$Foo because it was already configured on one or more child injectors or private modules.
1) [Guice/ChildBindingAlreadySet]: A binding for ChildBindingAlreadySetErrorTest$Foo already exists in one or more child injectors or private modules. Bindings from children can only be used by a parent if the child was a PrivateModule and the binding was exposed. Parent injectors cannot create bindings that already exist in a child.

Bound at:
1 : ChildBindingAlreadySetErrorTest$ChildModule.configure(ChildBindingAlreadySetErrorTest.java:34)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guice configuration errors:

1) [Guice/ChildBindingAlreadySet]: Unable to create binding for ChildBindingAlreadySetErrorTest$Foo because it was already configured on one or more child injectors or private modules.
1) [Guice/ChildBindingAlreadySet]: A binding for ChildBindingAlreadySetErrorTest$Foo already exists in one or more child injectors or private modules. Bindings from children can only be used by a parent if the child was a PrivateModule and the binding was exposed. Parent injectors cannot create bindings that already exist in a child.

Bound at:
1 : ChildBindingAlreadySetErrorTest$ChildModule.configure(ChildBindingAlreadySetErrorTest.java:34)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Unable to create injector, see the following errors:

1) [Guice/ChildBindingAlreadySet]: Unable to create binding for ChildBindingAlreadySetErrorTest$Foo because it was already configured on one or more child injectors or private modules.
1) [Guice/ChildBindingAlreadySet]: A binding for ChildBindingAlreadySetErrorTest$Foo already exists in one or more child injectors or private modules. Bindings from children can only be used by a parent if the child was a PrivateModule and the binding was exposed. Parent injectors cannot create bindings that already exist in a child.

Bound at:
1 : ChildBindingAlreadySetErrorTest$ChildModule.configure(ChildBindingAlreadySetErrorTest.java:34)
Expand Down

0 comments on commit bf43b4f

Please sign in to comment.