summaryrefslogtreecommitdiffstats
path: root/sangria-core/src
diff options
context:
space:
mode:
Diffstat (limited to 'sangria-core/src')
-rw-r--r--sangria-core/src/main/java/com/tavianator/sangria/core/DelayedError.java9
-rw-r--r--sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java29
2 files changed, 35 insertions, 3 deletions
diff --git a/sangria-core/src/main/java/com/tavianator/sangria/core/DelayedError.java b/sangria-core/src/main/java/com/tavianator/sangria/core/DelayedError.java
index d6f86a8..2aeabca 100644
--- a/sangria-core/src/main/java/com/tavianator/sangria/core/DelayedError.java
+++ b/sangria-core/src/main/java/com/tavianator/sangria/core/DelayedError.java
@@ -25,6 +25,8 @@ import com.google.inject.CreationException;
import com.google.inject.Injector;
import com.google.inject.spi.Message;
+import static com.google.common.base.Preconditions.*;
+
/**
* Similar to {@link Binder#addError(String, Object...)}, but can be canceled later. Useful for enforcing correct usage
* of fluent APIs.
@@ -35,6 +37,7 @@ import com.google.inject.spi.Message;
*/
public class DelayedError {
private Throwable error;
+ private boolean reported = false;
/**
* Create a {@link DelayedError}.
@@ -85,11 +88,13 @@ public class DelayedError {
* Cancel this error.
*/
public void cancel() {
- this.error = null;
+ checkState(!reported, "This error has already been reported");
+ error = null;
}
@Inject
- void inject(Injector injector) throws Throwable {
+ void reportErrors(Injector injector) throws Throwable {
+ reported = true;
if (error != null) {
throw error;
}
diff --git a/sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java b/sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java
index 349dced..f27aea8 100644
--- a/sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java
+++ b/sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java
@@ -32,7 +32,7 @@ import static org.hamcrest.Matchers.*;
* Tests for {@link DelayedError}.
*
* @author Tavian Barnes (tavianator@tavianator.com)
- * @version 1.0
+ * @version 1.1
* @since 1.0
*/
public class DelayedErrorTest {
@@ -83,4 +83,31 @@ public class DelayedErrorTest {
}
});
}
+
+ @Test
+ public void testCancel() {
+ Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ DelayedError error = DelayedError.create(binder(), "Message");
+ error.cancel();
+ }
+ });
+ }
+
+ @Test
+ public void testLateCancel() {
+ final DelayedError[] errorHolder = new DelayedError[1];
+
+ Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ errorHolder[0] = DelayedError.create(binder(), "Message");
+ errorHolder[0].cancel();
+ }
+ });
+
+ thrown.expect(IllegalStateException.class);
+ errorHolder[0].cancel();
+ }
}