From 76f67aebfef61d4ce0ca88747ee0d5a282acf143 Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Mon, 10 Nov 2014 11:53:10 -0200 Subject: [PATCH 01/13] refactoring exception handling code with a a Try class --- .../core/ToInstantiateInterceptorHandler.java | 15 ++- .../vraptor/observer/ExceptionHandler.java | 34 +++++++ .../vraptor/observer/ExecuteMethod.java | 94 +++++++++---------- .../java/br/com/caelum/vraptor/util/Try.java | 56 +++++++++++ .../vraptor/observer/ExecuteMethodTest.java | 38 ++++---- 5 files changed, 166 insertions(+), 71 deletions(-) create mode 100644 vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExceptionHandler.java create mode 100644 vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java index 3eac3dfdd..b90864bd4 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java @@ -19,6 +19,7 @@ import javax.enterprise.inject.Vetoed; +import br.com.caelum.vraptor.util.Try; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,6 +28,8 @@ import br.com.caelum.vraptor.interceptor.Interceptor; import br.com.caelum.vraptor.ioc.Container; +import java.util.concurrent.Callable; + /** * Instantiates the interceptor on the fly and executes its method. * @@ -46,16 +49,22 @@ public ToInstantiateInterceptorHandler(Container container, Class type) { } @Override - public void execute(InterceptorStack stack, ControllerMethod method, Object controllerInstance) + public void execute(final InterceptorStack stack, final ControllerMethod method, final Object controllerInstance) throws InterceptionException { - Interceptor interceptor = (Interceptor) container.instanceFor(type); + final Interceptor interceptor = (Interceptor) container.instanceFor(type); if (interceptor == null) { throw new InterceptionException("Unable to instantiate interceptor for " + type.getName() + ": the container returned null."); } if (interceptor.accepts(method)) { logger.debug("Invoking interceptor {}", interceptor.getClass().getSimpleName()); - interceptor.intercept(stack, method, controllerInstance); + Try result = Try.run(new Callable() { + @Override + public Void call() throws Exception { + interceptor.intercept(stack, method, controllerInstance); + return null; + } + }); } else { stack.next(method, controllerInstance); } diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExceptionHandler.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExceptionHandler.java new file mode 100644 index 000000000..bc000acd2 --- /dev/null +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExceptionHandler.java @@ -0,0 +1,34 @@ +package br.com.caelum.vraptor.observer; + +import br.com.caelum.vraptor.InterceptionException; +import br.com.caelum.vraptor.interceptor.ApplicationLogicException; +import br.com.caelum.vraptor.validator.ValidationException; +import net.vidageek.mirror.exception.ReflectionProviderException; +import org.slf4j.Logger; + +import static org.slf4j.LoggerFactory.getLogger; + +public class ExceptionHandler { + private final static Logger log = getLogger(ExceptionHandler.class); + + public void handle(Exception exception) { + if (exception instanceof IllegalArgumentException) { + throw new InterceptionException(exception); + } + if (exception instanceof ReflectionProviderException) { + throwIfNotValidationException(exception, exception.getCause()); + } + throwIfNotValidationException(exception, exception); + } + + private void throwIfNotValidationException(Throwable original, Throwable alternativeCause) { + Throwable cause = original.getCause(); + + if (original instanceof ValidationException || cause instanceof ValidationException) { + // fine... already parsed + log.trace("swallowing {}", cause); + } else { + throw new ApplicationLogicException(alternativeCause); + } + } +} diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java index d32c3e19e..51be1518e 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java @@ -17,28 +17,26 @@ package br.com.caelum.vraptor.observer; -import static org.slf4j.LoggerFactory.getLogger; - -import java.lang.reflect.Method; - -import javax.enterprise.context.Dependent; -import javax.enterprise.event.Event; -import javax.enterprise.event.Observes; -import javax.inject.Inject; - -import org.slf4j.Logger; - import br.com.caelum.vraptor.InterceptionException; import br.com.caelum.vraptor.controller.ControllerMethod; import br.com.caelum.vraptor.core.MethodInfo; import br.com.caelum.vraptor.core.ReflectionProvider; -import br.com.caelum.vraptor.core.ReflectionProviderException; import br.com.caelum.vraptor.events.InterceptorsExecuted; import br.com.caelum.vraptor.events.MethodExecuted; import br.com.caelum.vraptor.events.MethodReady; -import br.com.caelum.vraptor.interceptor.ApplicationLogicException; +import br.com.caelum.vraptor.util.Try; import br.com.caelum.vraptor.validator.Messages; -import br.com.caelum.vraptor.validator.ValidationException; +import net.vidageek.mirror.dsl.Mirror; +import org.slf4j.Logger; + +import javax.enterprise.context.Dependent; +import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import java.lang.reflect.Method; +import java.util.concurrent.Callable; + +import static org.slf4j.LoggerFactory.getLogger; /** * Interceptor that executes the logic method. @@ -58,49 +56,51 @@ public class ExecuteMethod { private final Event methodExecutedEvent; private final Event methodReady; + private final ExceptionHandler exceptionHandler; + + /** + * @deprecated CDI eyes only + */ + protected ExecuteMethod() { + this(null, null, null, null, null, null); + } @Inject - public ExecuteMethod(MethodInfo methodInfo, Messages messages, Event methodExecutedEvent, - Event methodReady, ReflectionProvider reflectionProvider) { + public ExecuteMethod(MethodInfo methodInfo, Messages messages, + Event methodExecutedEvent, Event methodReady, + ExceptionHandler exceptionHandler, ReflectionProvider reflectionProvider) { this.methodInfo = methodInfo; this.messages = messages; this.methodExecutedEvent = methodExecutedEvent; this.methodReady = methodReady; + this.exceptionHandler = exceptionHandler; this.reflectionProvider = reflectionProvider; } - public void execute(@Observes InterceptorsExecuted event) { - try { - ControllerMethod method = event.getControllerMethod(); - methodReady.fire(new MethodReady(method)); - Method reflectionMethod = method .getMethod(); - Object[] parameters = methodInfo.getParametersValues(); - - log.debug("Invoking {}", reflectionMethod); - Object instance = event.getControllerInstance(); - Object result = reflectionProvider.invoke(instance, reflectionMethod, parameters); - - messages.assertAbsenceOfErrors(); - - this.methodInfo.setResult(result); - methodExecutedEvent.fire(new MethodExecuted(method, methodInfo)); - } catch (IllegalArgumentException e) { - throw new InterceptionException(e); - } catch (ReflectionProviderException e) { - throwIfNotValidationException(e, e.getCause()); - } catch (Exception e) { - throwIfNotValidationException(e, e); + public void execute(@Observes final InterceptorsExecuted event) { + Try run = Try.run(new Callable() { + @Override + public Void call() throws Exception { + ControllerMethod method = event.getControllerMethod(); + methodReady.fire(new MethodReady(method)); + Method reflectionMethod = method.getMethod(); + Object[] parameters = methodInfo.getParametersValues(); + + log.debug("Invoking {}", reflectionMethod); + Object instance = event.getControllerInstance(); + Object result = reflectionProvider.invoke(instance, reflectionMethod, parameters); + + messages.assertAbsenceOfErrors(); + + methodInfo.setResult(result); + methodExecutedEvent.fire(new MethodExecuted(method, methodInfo)); + return null; + } + }); + if (run.failed()) { + Exception exception = run.getException(); + exceptionHandler.handle(exception); } } - private void throwIfNotValidationException(Throwable original, Throwable alternativeCause) { - Throwable cause = original.getCause(); - - if (original instanceof ValidationException || cause instanceof ValidationException) { - // fine... already parsed - log.trace("swallowing {}", cause); - } else { - throw new ApplicationLogicException(alternativeCause); - } - } } \ No newline at end of file diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java new file mode 100644 index 000000000..27b4c71b2 --- /dev/null +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java @@ -0,0 +1,56 @@ +package br.com.caelum.vraptor.util; + +import java.util.concurrent.Callable; + +public abstract class Try { + public static Try run(Callable callable) { + try { + Void call = callable.call(); + return new Success(call); + } catch (Exception e) { + return new Failed(e); + } + } + + public abstract boolean failed(); + + public abstract Exception getException(); + + public static class Success extends Try { + private final T result; + + public Success(T result) { + this.result = result; + } + + @Override + public boolean failed() { + return false; + } + + @Override + public Exception getException() { + throw new UnsupportedOperationException(); + } + } + + public static class Failed extends Try { + private final Exception e; + + public Failed(Exception e) { + this.e = e; + } + + @Override + public boolean failed() { + return true; + } + + @Override + public Exception getException() { + return e; + } + } + + +} diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java index e5363c422..9d48d7a5c 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java @@ -17,26 +17,6 @@ package br.com.caelum.vraptor.observer; -import static br.com.caelum.vraptor.controller.DefaultControllerMethod.instanceFor; -import static br.com.caelum.vraptor.view.Results.nothing; -import static org.hamcrest.Matchers.any; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.lang.reflect.Method; -import java.util.Collections; - -import javax.enterprise.event.Event; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - import br.com.caelum.vraptor.InterceptionException; import br.com.caelum.vraptor.controller.ControllerMethod; import br.com.caelum.vraptor.controller.DefaultControllerMethod; @@ -51,6 +31,21 @@ import br.com.caelum.vraptor.validator.Messages; import br.com.caelum.vraptor.validator.ValidationException; import br.com.caelum.vraptor.validator.Validator; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import javax.enterprise.event.Event; +import java.lang.reflect.Method; +import java.util.Collections; + +import static br.com.caelum.vraptor.controller.DefaultControllerMethod.instanceFor; +import static br.com.caelum.vraptor.view.Results.nothing; +import static org.hamcrest.Matchers.any; +import static org.mockito.Mockito.*; public class ExecuteMethodTest { @@ -68,7 +63,8 @@ public class ExecuteMethodTest { @Before public void setup() throws NoSuchMethodException { MockitoAnnotations.initMocks(this); - observer = new ExecuteMethod(methodInfo, messages, methodEvecutedEvent, readyToExecuteMethodEvent, new DefaultReflectionProvider()); + observer = new ExecuteMethod(methodInfo, messages, methodEvecutedEvent, readyToExecuteMethodEvent, + new ExceptionHandler(), new DefaultReflectionProvider()); } @Test From f7cd9035be9a373fb75f50a464094fd1ff018499 Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Tue, 11 Nov 2014 15:48:53 -0200 Subject: [PATCH 02/13] Try should be generic --- .../java/br/com/caelum/vraptor/util/Try.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java index 27b4c71b2..ad4e732a8 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java @@ -2,10 +2,10 @@ import java.util.concurrent.Callable; -public abstract class Try { - public static Try run(Callable callable) { +public abstract class Try { + public static Try run(Callable callable) { try { - Void call = callable.call(); + T call = callable.call(); return new Success(call); } catch (Exception e) { return new Failed(e); @@ -14,6 +14,8 @@ public static Try run(Callable callable) { public abstract boolean failed(); + public abstract T value(); + public abstract Exception getException(); public static class Success extends Try { @@ -28,6 +30,11 @@ public boolean failed() { return false; } + @Override + public Object value() { + return result; + } + @Override public Exception getException() { throw new UnsupportedOperationException(); @@ -46,6 +53,11 @@ public boolean failed() { return true; } + @Override + public Object value() { + throw new UnsupportedOperationException(); + } + @Override public Exception getException() { return e; From 4b17d6c83b7c4872536a1faa7a5cb3ccc04d7ec3 Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Tue, 11 Nov 2014 15:52:32 -0200 Subject: [PATCH 03/13] renaming method and adding exception message --- .../src/main/java/br/com/caelum/vraptor/util/Try.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java index ad4e732a8..fc7616dc5 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java @@ -14,7 +14,7 @@ public static Try run(Callable callable) { public abstract boolean failed(); - public abstract T value(); + public abstract T result(); public abstract Exception getException(); @@ -31,13 +31,13 @@ public boolean failed() { } @Override - public Object value() { + public Object result() { return result; } @Override public Exception getException() { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("A Success doesn't have an exception."); } } @@ -54,8 +54,8 @@ public boolean failed() { } @Override - public Object value() { - throw new UnsupportedOperationException(); + public Object result() { + throw new UnsupportedOperationException("A Failed doesn't have a result."); } @Override From b64e749ebc53312a2582c6179f000e2ebe73c0dc Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Tue, 11 Nov 2014 15:55:32 -0200 Subject: [PATCH 04/13] renaming class --- .../br/com/caelum/vraptor/observer/ExecuteMethod.java | 10 ++++------ ...Handler.java => ExecuteMethodExceptionHandler.java} | 4 ++-- ... ExecuteMethodExceptionHandlerInterceptorTest.java} | 2 +- .../com/caelum/vraptor/observer/ExecuteMethodTest.java | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) rename vraptor-core/src/main/java/br/com/caelum/vraptor/observer/{ExceptionHandler.java => ExecuteMethodExceptionHandler.java} (89%) rename vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/{ExceptionHandlerInterceptorTest.java => ExecuteMethodExceptionHandlerInterceptorTest.java} (97%) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java index 51be1518e..8d67a441f 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java @@ -17,7 +17,6 @@ package br.com.caelum.vraptor.observer; -import br.com.caelum.vraptor.InterceptionException; import br.com.caelum.vraptor.controller.ControllerMethod; import br.com.caelum.vraptor.core.MethodInfo; import br.com.caelum.vraptor.core.ReflectionProvider; @@ -26,7 +25,6 @@ import br.com.caelum.vraptor.events.MethodReady; import br.com.caelum.vraptor.util.Try; import br.com.caelum.vraptor.validator.Messages; -import net.vidageek.mirror.dsl.Mirror; import org.slf4j.Logger; import javax.enterprise.context.Dependent; @@ -56,7 +54,7 @@ public class ExecuteMethod { private final Event methodExecutedEvent; private final Event methodReady; - private final ExceptionHandler exceptionHandler; + private final ExecuteMethodExceptionHandler executeMethodExceptionHandler; /** * @deprecated CDI eyes only @@ -68,12 +66,12 @@ protected ExecuteMethod() { @Inject public ExecuteMethod(MethodInfo methodInfo, Messages messages, Event methodExecutedEvent, Event methodReady, - ExceptionHandler exceptionHandler, ReflectionProvider reflectionProvider) { + ExecuteMethodExceptionHandler exceptionHandler, ReflectionProvider reflectionProvider) { this.methodInfo = methodInfo; this.messages = messages; this.methodExecutedEvent = methodExecutedEvent; this.methodReady = methodReady; - this.exceptionHandler = exceptionHandler; + this.executeMethodExceptionHandler = exceptionHandler; this.reflectionProvider = reflectionProvider; } @@ -99,7 +97,7 @@ public Void call() throws Exception { }); if (run.failed()) { Exception exception = run.getException(); - exceptionHandler.handle(exception); + executeMethodExceptionHandler.handle(exception); } } diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExceptionHandler.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java similarity index 89% rename from vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExceptionHandler.java rename to vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java index bc000acd2..4edec42a4 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExceptionHandler.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java @@ -8,8 +8,8 @@ import static org.slf4j.LoggerFactory.getLogger; -public class ExceptionHandler { - private final static Logger log = getLogger(ExceptionHandler.class); +public class ExecuteMethodExceptionHandler { + private final static Logger log = getLogger(ExecuteMethodExceptionHandler.class); public void handle(Exception exception) { if (exception instanceof IllegalArgumentException) { diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/ExceptionHandlerInterceptorTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/ExecuteMethodExceptionHandlerInterceptorTest.java similarity index 97% rename from vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/ExceptionHandlerInterceptorTest.java rename to vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/ExecuteMethodExceptionHandlerInterceptorTest.java index f5ce54492..f14fa2303 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/ExceptionHandlerInterceptorTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/ExecuteMethodExceptionHandlerInterceptorTest.java @@ -34,7 +34,7 @@ import br.com.caelum.vraptor.core.ExceptionRecorder; import br.com.caelum.vraptor.core.InterceptorStack; -public class ExceptionHandlerInterceptorTest { +public class ExecuteMethodExceptionHandlerInterceptorTest { private Object instance; @Mock private InterceptorStack stack; diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java index 9d48d7a5c..deaf8eca4 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java @@ -64,7 +64,7 @@ public class ExecuteMethodTest { public void setup() throws NoSuchMethodException { MockitoAnnotations.initMocks(this); observer = new ExecuteMethod(methodInfo, messages, methodEvecutedEvent, readyToExecuteMethodEvent, - new ExceptionHandler(), new DefaultReflectionProvider()); + new ExecuteMethodExceptionHandler(), new DefaultReflectionProvider()); } @Test From eadf45d58fbfc1b85d4dff059b58b2a3c83ea305 Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Tue, 11 Nov 2014 16:28:44 -0200 Subject: [PATCH 05/13] handling validation exception around interceptors --- .../DefaultInterceptorHandlerFactory.java | 10 ++- .../core/ToInstantiateInterceptorHandler.java | 32 ++++++--- .../validator/ValidatedInterceptor.java | 11 ++++ .../DefaultInterceptorHandlerFactoryTest.java | 4 +- .../ToInstantiateInterceptorHandlerTest.java | 65 +++++++++++++++++-- 5 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 vraptor-core/src/main/java/br/com/caelum/vraptor/validator/ValidatedInterceptor.java diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/DefaultInterceptorHandlerFactory.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/DefaultInterceptorHandlerFactory.java index 957df1fda..ea50ef485 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/DefaultInterceptorHandlerFactory.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/DefaultInterceptorHandlerFactory.java @@ -28,6 +28,7 @@ import br.com.caelum.vraptor.interceptor.StepInvoker; import br.com.caelum.vraptor.ioc.Container; +import br.com.caelum.vraptor.observer.ExecuteMethodExceptionHandler; import com.google.common.base.Supplier; /** @@ -44,18 +45,20 @@ public class DefaultInterceptorHandlerFactory implements InterceptorHandlerFacto private final InterceptorAcceptsExecutor acceptsExecutor; private final CustomAcceptsExecutor customAcceptsExecutor; private final InterceptorExecutor interceptorExecutor; + private final ExecuteMethodExceptionHandler executeMethodExceptionHandler; /** * @deprecated CDI eyes only */ protected DefaultInterceptorHandlerFactory() { - this(null, null, null, null, null, null); + this(null, null, null, null, null, null, null); } @Inject public DefaultInterceptorHandlerFactory(Container container, StepInvoker stepInvoker, CacheStore, InterceptorHandler> cachedHandlers, InterceptorAcceptsExecutor acceptsExecutor, - CustomAcceptsExecutor customAcceptsExecutor, InterceptorExecutor interceptorExecutor) { + CustomAcceptsExecutor customAcceptsExecutor, InterceptorExecutor interceptorExecutor, + ExecuteMethodExceptionHandler executeMethodExceptionHandler) { this.container = container; this.stepInvoker = stepInvoker; @@ -63,6 +66,7 @@ public DefaultInterceptorHandlerFactory(Container container, StepInvoker stepInv this.acceptsExecutor = acceptsExecutor; this.customAcceptsExecutor = customAcceptsExecutor; this.interceptorExecutor = interceptorExecutor; + this.executeMethodExceptionHandler = executeMethodExceptionHandler; } @Override @@ -74,7 +78,7 @@ public InterceptorHandler get() { return new AspectStyleInterceptorHandler(type, stepInvoker, container, customAcceptsExecutor, acceptsExecutor, interceptorExecutor); } - return new ToInstantiateInterceptorHandler(container, type); + return new ToInstantiateInterceptorHandler(container, type, executeMethodExceptionHandler); } }); } diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java index b90864bd4..ccf9eee16 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java @@ -19,7 +19,9 @@ import javax.enterprise.inject.Vetoed; +import br.com.caelum.vraptor.observer.ExecuteMethodExceptionHandler; import br.com.caelum.vraptor.util.Try; +import br.com.caelum.vraptor.validator.ValidatedInterceptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,10 +44,12 @@ public class ToInstantiateInterceptorHandler implements InterceptorHandler { private final Container container; private final Class type; + private final ExecuteMethodExceptionHandler executeMethodExceptionHandler; - public ToInstantiateInterceptorHandler(Container container, Class type) { + public ToInstantiateInterceptorHandler(Container container, Class type, ExecuteMethodExceptionHandler executeMethodExceptionHandler) { this.container = container; this.type = type; + this.executeMethodExceptionHandler = executeMethodExceptionHandler; } @Override @@ -58,18 +62,30 @@ public void execute(final InterceptorStack stack, final ControllerMethod method, } if (interceptor.accepts(method)) { logger.debug("Invoking interceptor {}", interceptor.getClass().getSimpleName()); - Try result = Try.run(new Callable() { - @Override - public Void call() throws Exception { - interceptor.intercept(stack, method, controllerInstance); - return null; - } - }); + boolean isValidated = type.isAnnotationPresent(ValidatedInterceptor.class); + if (isValidated) { + executeSafely(stack, method, controllerInstance, interceptor); + } else { + interceptor.intercept(stack, method, controllerInstance); + } } else { stack.next(method, controllerInstance); } } + private void executeSafely(final InterceptorStack stack, final ControllerMethod method, final Object controllerInstance, final Interceptor interceptor) { + Try result = Try.run(new Callable() { + @Override + public Void call() throws Exception { + interceptor.intercept(stack, method, controllerInstance); + return null; + } + }); + if (result.failed()) { + executeMethodExceptionHandler.handle(result.getException()); + } + } + @Override public String toString() { return "ToInstantiateHandler for " + type.getName(); diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/ValidatedInterceptor.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/ValidatedInterceptor.java new file mode 100644 index 000000000..55b267911 --- /dev/null +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/ValidatedInterceptor.java @@ -0,0 +1,11 @@ +package br.com.caelum.vraptor.validator; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface ValidatedInterceptor { +} diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/core/DefaultInterceptorHandlerFactoryTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/core/DefaultInterceptorHandlerFactoryTest.java index 56742c550..976e8182d 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/core/DefaultInterceptorHandlerFactoryTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/core/DefaultInterceptorHandlerFactoryTest.java @@ -20,6 +20,7 @@ import static org.hamcrest.Matchers.sameInstance; import static org.junit.Assert.assertThat; +import br.com.caelum.vraptor.observer.ExecuteMethodExceptionHandler; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -55,8 +56,9 @@ public void setUp() throws Exception { MockitoAnnotations.initMocks(this); CacheStore, InterceptorHandler> cachedHandlers = new DefaultCacheStore<>(); + ExecuteMethodExceptionHandler executeMethodExceptionHandler = new ExecuteMethodExceptionHandler(); factory = new DefaultInterceptorHandlerFactory(container, stepInvoker, - cachedHandlers, acceptsExecutor, customAcceptsExecutor, interceptorExecutor); + cachedHandlers, acceptsExecutor, customAcceptsExecutor, interceptorExecutor, executeMethodExceptionHandler); } static interface RegularInterceptor extends Interceptor {} diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java index 54d7b4e9c..5b5dfdd47 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java @@ -16,18 +16,26 @@ */ package br.com.caelum.vraptor.core; +import static org.hamcrest.CoreMatchers.any; import static org.hamcrest.CoreMatchers.containsString; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.IOException; +import java.util.ArrayList; +import br.com.caelum.vraptor.observer.ExecuteMethodExceptionHandler; +import br.com.caelum.vraptor.validator.Message; +import br.com.caelum.vraptor.validator.ValidatedInterceptor; +import br.com.caelum.vraptor.validator.ValidationException; +import com.google.common.collect.Lists; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import br.com.caelum.vraptor.InterceptionException; @@ -77,7 +85,7 @@ public void shouldComplainWhenUnableToInstantiateAnInterceptor() throws Intercep when(container.instanceFor(MyWeirdInterceptor.class)).thenReturn(null); ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, - MyWeirdInterceptor.class); + MyWeirdInterceptor.class, new ExecuteMethodExceptionHandler()); handler.execute(null, null, null); } @@ -88,7 +96,7 @@ public void shouldInvokeInterceptorsMethodIfAbleToInstantiateIt() throws Interce when(container.instanceFor(Interceptor.class)).thenReturn(interceptor); when(interceptor.accepts(method)).thenReturn(true); - ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, Interceptor.class); + ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, Interceptor.class, new ExecuteMethodExceptionHandler()); handler.execute(stack, method, instance); verify(interceptor).intercept(stack, method, instance); @@ -99,10 +107,59 @@ public void shouldNotInvokeInterceptorsMethodIfInterceptorDoesntAcceptsResource( when(container.instanceFor(Interceptor.class)).thenReturn(interceptor); when(interceptor.accepts(method)).thenReturn(false); - ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, Interceptor.class); + ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, Interceptor.class, new ExecuteMethodExceptionHandler()); handler.execute(stack, method, instance); verify(interceptor, never()).intercept(stack, method, instance); verify(stack).next(method, instance); - } + } + + @Test + public void shouldCatchValidationExceptionOfValidatedInterceptor() { + MyValidatedInterceptor validatedInterceptor = new MyValidatedInterceptor(); + when(container.instanceFor(MyValidatedInterceptor.class)).thenReturn(validatedInterceptor); + ExecuteMethodExceptionHandler exceptionHandler = Mockito.spy(new ExecuteMethodExceptionHandler()); + ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, MyValidatedInterceptor.class, exceptionHandler); + + handler.execute(stack, method, new Object()); + verify(exceptionHandler).handle(Mockito.any(ValidationException.class)); + } + + @Test + public void shouldNotCatchValidationExceptionOfRegularInterceptor() { + MyBuggedInterceptor validatedInterceptor = new MyBuggedInterceptor(); + when(container.instanceFor(MyBuggedInterceptor.class)).thenReturn(validatedInterceptor); + ExecuteMethodExceptionHandler exceptionHandler = new ExecuteMethodExceptionHandler(); + ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, MyBuggedInterceptor.class, exceptionHandler); + + exception.expect(ValidationException.class); + handler.execute(stack, method, new Object()); + } + + @ValidatedInterceptor + public static class MyValidatedInterceptor implements Interceptor { + + @Override + public void intercept(InterceptorStack stack, ControllerMethod method, Object resourceInstance) + throws InterceptionException { + throw new ValidationException(new ArrayList()); + } + + @Override + public boolean accepts(ControllerMethod method) { + return true; + } + } + + private static class MyBuggedInterceptor implements Interceptor { + @Override + public void intercept(InterceptorStack stack, ControllerMethod method, Object controllerInstance) throws InterceptionException { + throw new ValidationException(new ArrayList()); + } + + @Override + public boolean accepts(ControllerMethod method) { + return true; + } + } } From 646fc1f3089e08dd0e31bb9073629f4430d70ed8 Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Mon, 22 Dec 2014 13:57:51 -0200 Subject: [PATCH 06/13] removing empty lines --- vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java index fc7616dc5..9ade29d27 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java @@ -64,5 +64,4 @@ public Exception getException() { } } - -} +} \ No newline at end of file From 3603996b60a20fad564c8293ec224d4963e1f404 Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Mon, 22 Dec 2014 14:04:14 -0200 Subject: [PATCH 07/13] catching validation errors for all interceptors and removing annotation --- .../core/ToInstantiateInterceptorHandler.java | 20 +++---- .../vraptor/observer/ExecuteMethod.java | 2 +- .../ExecuteMethodExceptionHandler.java | 5 ++ .../validator/ValidatedInterceptor.java | 11 ---- .../ToInstantiateInterceptorHandlerTest.java | 52 ++++--------------- 5 files changed, 22 insertions(+), 68 deletions(-) delete mode 100644 vraptor-core/src/main/java/br/com/caelum/vraptor/validator/ValidatedInterceptor.java diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java index ccf9eee16..afdd33b7f 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java @@ -17,19 +17,16 @@ package br.com.caelum.vraptor.core; -import javax.enterprise.inject.Vetoed; - -import br.com.caelum.vraptor.observer.ExecuteMethodExceptionHandler; -import br.com.caelum.vraptor.util.Try; -import br.com.caelum.vraptor.validator.ValidatedInterceptor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import br.com.caelum.vraptor.InterceptionException; import br.com.caelum.vraptor.controller.ControllerMethod; import br.com.caelum.vraptor.interceptor.Interceptor; import br.com.caelum.vraptor.ioc.Container; +import br.com.caelum.vraptor.observer.ExecuteMethodExceptionHandler; +import br.com.caelum.vraptor.util.Try; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import javax.enterprise.inject.Vetoed; import java.util.concurrent.Callable; /** @@ -62,12 +59,7 @@ public void execute(final InterceptorStack stack, final ControllerMethod method, } if (interceptor.accepts(method)) { logger.debug("Invoking interceptor {}", interceptor.getClass().getSimpleName()); - boolean isValidated = type.isAnnotationPresent(ValidatedInterceptor.class); - if (isValidated) { - executeSafely(stack, method, controllerInstance, interceptor); - } else { - interceptor.intercept(stack, method, controllerInstance); - } + executeSafely(stack, method, controllerInstance, interceptor); } else { stack.next(method, controllerInstance); } diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java index 8d67a441f..a4269eb18 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java @@ -37,7 +37,7 @@ import static org.slf4j.LoggerFactory.getLogger; /** - * Interceptor that executes the logic method. + * Observer that executes the logic method. * * @author Guilherme Silveira * @author Rodrigo Turini diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java index 4edec42a4..df22b4356 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java @@ -8,6 +8,11 @@ import static org.slf4j.LoggerFactory.getLogger; +/** + * Handles exceptions thrown by a controller method + * + * @author Chico Sokol + */ public class ExecuteMethodExceptionHandler { private final static Logger log = getLogger(ExecuteMethodExceptionHandler.class); diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/ValidatedInterceptor.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/ValidatedInterceptor.java deleted file mode 100644 index 55b267911..000000000 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/ValidatedInterceptor.java +++ /dev/null @@ -1,11 +0,0 @@ -package br.com.caelum.vraptor.validator; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -public @interface ValidatedInterceptor { -} diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java index 5b5dfdd47..9198629e9 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java @@ -16,20 +16,13 @@ */ package br.com.caelum.vraptor.core; -import static org.hamcrest.CoreMatchers.any; -import static org.hamcrest.CoreMatchers.containsString; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.io.IOException; -import java.util.ArrayList; - +import br.com.caelum.vraptor.InterceptionException; +import br.com.caelum.vraptor.controller.ControllerMethod; +import br.com.caelum.vraptor.interceptor.Interceptor; +import br.com.caelum.vraptor.ioc.Container; import br.com.caelum.vraptor.observer.ExecuteMethodExceptionHandler; import br.com.caelum.vraptor.validator.Message; -import br.com.caelum.vraptor.validator.ValidatedInterceptor; import br.com.caelum.vraptor.validator.ValidationException; -import com.google.common.collect.Lists; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -38,10 +31,11 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import br.com.caelum.vraptor.InterceptionException; -import br.com.caelum.vraptor.controller.ControllerMethod; -import br.com.caelum.vraptor.interceptor.Interceptor; -import br.com.caelum.vraptor.ioc.Container; +import java.io.IOException; +import java.util.ArrayList; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.mockito.Mockito.*; public class ToInstantiateInterceptorHandlerTest { @@ -125,33 +119,7 @@ public void shouldCatchValidationExceptionOfValidatedInterceptor() { verify(exceptionHandler).handle(Mockito.any(ValidationException.class)); } - @Test - public void shouldNotCatchValidationExceptionOfRegularInterceptor() { - MyBuggedInterceptor validatedInterceptor = new MyBuggedInterceptor(); - when(container.instanceFor(MyBuggedInterceptor.class)).thenReturn(validatedInterceptor); - ExecuteMethodExceptionHandler exceptionHandler = new ExecuteMethodExceptionHandler(); - ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, MyBuggedInterceptor.class, exceptionHandler); - - exception.expect(ValidationException.class); - handler.execute(stack, method, new Object()); - } - - @ValidatedInterceptor - public static class MyValidatedInterceptor implements Interceptor { - - @Override - public void intercept(InterceptorStack stack, ControllerMethod method, Object resourceInstance) - throws InterceptionException { - throw new ValidationException(new ArrayList()); - } - - @Override - public boolean accepts(ControllerMethod method) { - return true; - } - } - - private static class MyBuggedInterceptor implements Interceptor { + private static class MyValidatedInterceptor implements Interceptor { @Override public void intercept(InterceptorStack stack, ControllerMethod method, Object controllerInstance) throws InterceptionException { throw new ValidationException(new ArrayList()); From 9c1ee3e9b0007321e7fa02073d9b47129ad6e233 Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Mon, 22 Dec 2014 15:09:16 -0200 Subject: [PATCH 08/13] fixing import --- .../caelum/vraptor/observer/ExecuteMethodExceptionHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java index df22b4356..550cfe664 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethodExceptionHandler.java @@ -1,9 +1,9 @@ package br.com.caelum.vraptor.observer; import br.com.caelum.vraptor.InterceptionException; +import br.com.caelum.vraptor.core.ReflectionProviderException; import br.com.caelum.vraptor.interceptor.ApplicationLogicException; import br.com.caelum.vraptor.validator.ValidationException; -import net.vidageek.mirror.exception.ReflectionProviderException; import org.slf4j.Logger; import static org.slf4j.LoggerFactory.getLogger; From ad4b568c641657bbbf3f0e8eecdddaae94e14573 Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Mon, 29 Dec 2014 11:33:17 -0200 Subject: [PATCH 09/13] removing pointless constructor --- .../java/br/com/caelum/vraptor/observer/ExecuteMethod.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java index a4269eb18..c4d9d6f81 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java @@ -56,13 +56,6 @@ public class ExecuteMethod { private final Event methodReady; private final ExecuteMethodExceptionHandler executeMethodExceptionHandler; - /** - * @deprecated CDI eyes only - */ - protected ExecuteMethod() { - this(null, null, null, null, null, null); - } - @Inject public ExecuteMethod(MethodInfo methodInfo, Messages messages, Event methodExecutedEvent, Event methodReady, From 37d89fd504e13e2369fadd6c900bbc1bad8fa15e Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Mon, 29 Dec 2014 11:46:50 -0200 Subject: [PATCH 10/13] moving try to core --- .../src/main/java/br/com/caelum/vraptor/{util => core}/Try.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename vraptor-core/src/main/java/br/com/caelum/vraptor/{util => core}/Try.java (96%) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/Try.java similarity index 96% rename from vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java rename to vraptor-core/src/main/java/br/com/caelum/vraptor/core/Try.java index 9ade29d27..150d9be68 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/util/Try.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/Try.java @@ -1,4 +1,4 @@ -package br.com.caelum.vraptor.util; +package br.com.caelum.vraptor.core; import java.util.concurrent.Callable; From 7d8b1f5338e7fbfc72064e23d3ff04ff5157861a Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Mon, 29 Dec 2014 11:57:40 -0200 Subject: [PATCH 11/13] Try javadoc --- .../java/br/com/caelum/vraptor/core/Try.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/Try.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/Try.java index 150d9be68..1fe8fbc5f 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/Try.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/Try.java @@ -2,6 +2,25 @@ import java.util.concurrent.Callable; +/** + * A class to wrap code that can possibly throw exceptions. + * + * Use the static method Try#run to instantiate this class, passing down + * the dangerous code and use its methods to retrieve the result or the exception + * of your computation. Example using java 8: + * + * + * Try try = Try.run(() -> someDangerousMethod()); + * if (try.failed()) { + * Exception e = try.getException(); + * handleError(e); + * } + * try.result(); //do something with the result + * + * + * @author Chico Sokol + * @param the type of the result of your computation + */ public abstract class Try { public static Try run(Callable callable) { try { @@ -21,7 +40,7 @@ public static Try run(Callable callable) { public static class Success extends Try { private final T result; - public Success(T result) { + private Success(T result) { this.result = result; } @@ -44,7 +63,7 @@ public Exception getException() { public static class Failed extends Try { private final Exception e; - public Failed(Exception e) { + private Failed(Exception e) { this.e = e; } From 59d03b50a5d7115c333a61d8d06cc910f81b2a02 Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Mon, 29 Dec 2014 12:21:43 -0200 Subject: [PATCH 12/13] fixing imports --- .../caelum/vraptor/core/ToInstantiateInterceptorHandler.java | 1 - .../main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java index afdd33b7f..f90f119bf 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandler.java @@ -22,7 +22,6 @@ import br.com.caelum.vraptor.interceptor.Interceptor; import br.com.caelum.vraptor.ioc.Container; import br.com.caelum.vraptor.observer.ExecuteMethodExceptionHandler; -import br.com.caelum.vraptor.util.Try; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java index c4d9d6f81..259c536a4 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/ExecuteMethod.java @@ -20,10 +20,10 @@ import br.com.caelum.vraptor.controller.ControllerMethod; import br.com.caelum.vraptor.core.MethodInfo; import br.com.caelum.vraptor.core.ReflectionProvider; +import br.com.caelum.vraptor.core.Try; import br.com.caelum.vraptor.events.InterceptorsExecuted; import br.com.caelum.vraptor.events.MethodExecuted; import br.com.caelum.vraptor.events.MethodReady; -import br.com.caelum.vraptor.util.Try; import br.com.caelum.vraptor.validator.Messages; import org.slf4j.Logger; From 9503b57f358b415609873c71c44bf749b608ab5f Mon Sep 17 00:00:00 2001 From: Francisco Sokol Date: Mon, 29 Dec 2014 12:22:01 -0200 Subject: [PATCH 13/13] Try docs on vraptor-site --- vraptor-site/content/en/docs/components.html | 17 +++++++++++++++++ vraptor-site/content/pt/docs/componentes.html | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/vraptor-site/content/en/docs/components.html b/vraptor-site/content/en/docs/components.html index f2f96d0ba..e0a7b499b 100644 --- a/vraptor-site/content/en/docs/components.html +++ b/vraptor-site/content/en/docs/components.html @@ -478,3 +478,20 @@ ~~~ Thus, when the data of an object are not present in the request, you will receive a `null` parameter instead of an empty instance. + +## Using the Try class + +You can use the class `Try` from the public API of VRaptor to handle exceptions more easily. + +With this class, you can specify the code that might throw exceptions with the `run` method: + +~~~ +Try try = Try.run(() -> aDangerousMethod()); +if (try.failed()) { + Exception e = try.getException(); + handleError(e); +} +handleResult(try.result()); +~~~ + +This class is really useful to compose several computations that can throw exceptions. \ No newline at end of file diff --git a/vraptor-site/content/pt/docs/componentes.html b/vraptor-site/content/pt/docs/componentes.html index e1d275525..c191f3e5b 100644 --- a/vraptor-site/content/pt/docs/componentes.html +++ b/vraptor-site/content/pt/docs/componentes.html @@ -397,3 +397,20 @@ ~~~ Dessa forma, quando os dados de um objeto não estiverem presentes no request, você receberá um parâmetro `null` no lugar de uma instância vazia. + +## Usando a classe Try + +Para facilitar o tratamento de exceções, você pode usar a classe `Try`, da API pública do VRaptor. + +Com essa classe, você pode especificar a código que pode lançar exceptions dentro do método `run`: + +~~~ +Try try = Try.run(() -> umMetodoPerigoso()); +if (try.failed()) { + Exception e = try.getException(); + lidaComErro(e); +} +lidaComResultado(try.result()); +~~~ + +Essa classe é especialmente útil para compor diferentes trechos de código que lançam exceptions. \ No newline at end of file