Skip to content

Commit

Permalink
Merge pull request #945 from caelum/issue_911
Browse files Browse the repository at this point in the history
propagating alternative cause if it's not checked
  • Loading branch information
Turini committed Feb 25, 2015
2 parents 5a79e78 + c92a1a3 commit 695c1f4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package br.com.caelum.vraptor.observer;

import br.com.caelum.vraptor.InterceptionException;
import static org.slf4j.LoggerFactory.getLogger;

import org.slf4j.Logger;

import br.com.caelum.vraptor.core.ReflectionProviderException;
import br.com.caelum.vraptor.interceptor.ApplicationLogicException;
import br.com.caelum.vraptor.validator.ValidationException;
import org.slf4j.Logger;

import static org.slf4j.LoggerFactory.getLogger;
import com.google.common.base.Throwables;

/**
* Handles exceptions thrown by a controller method
Expand All @@ -17,9 +19,6 @@ public class ExecuteMethodExceptionHandler {
private final static Logger log = getLogger(ExecuteMethodExceptionHandler.class);

public void handle(Exception exception) {
if (exception instanceof IllegalArgumentException) {
throw new InterceptionException(exception);
}
if (exception instanceof ReflectionProviderException) {
throwIfNotValidationException(exception, exception.getCause());
}
Expand All @@ -33,6 +32,7 @@ private void throwIfNotValidationException(Throwable original, Throwable alterna
// fine... already parsed
log.trace("swallowing {}", cause);
} else {
Throwables.propagateIfPossible(alternativeCause);
throw new ApplicationLogicException(alternativeCause);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void assertAbsenceOfErrors() {
if (hasUnhandledErrors()) {
log.debug("Some validation errors occured: {}", getErrors());

throw new IllegalStateException(
throw new ValidationFailedException(
"There are validation errors and you forgot to specify where to go. Please add in your method "
+ "something like:\n"
+ "validator.onErrorUse(page()).of(AnyController.class).anyMethod();\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package br.com.caelum.vraptor.validator;

import javax.enterprise.inject.Vetoed;

import br.com.caelum.vraptor.VRaptorException;

@Vetoed
public class ValidationFailedException extends VRaptorException {

private static final long serialVersionUID = 3495204717080278982L;

public ValidationFailedException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
import br.com.caelum.vraptor.interceptor.DogAlike;
import br.com.caelum.vraptor.validator.Message;
import br.com.caelum.vraptor.validator.Messages;
import br.com.caelum.vraptor.validator.ValidationFailedException;
import br.com.caelum.vraptor.validator.ValidationException;
import br.com.caelum.vraptor.validator.ValidationFailedException;
import br.com.caelum.vraptor.validator.Validator;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -39,6 +42,7 @@
import org.mockito.MockitoAnnotations;

import javax.enterprise.event.Event;

import java.lang.reflect.Method;
import java.util.Collections;

Expand Down Expand Up @@ -126,29 +130,39 @@ public void shouldBeOkIfThereIsValidationErrorsAndYouSpecifiedWhereToGo() throws

@Test
public void shouldThrowExceptionIfYouHaventSpecifiedWhereToGoOnValidationError() throws Exception {
exception.expect(InterceptionException.class);
exception.expect(ValidationFailedException.class);

Method didntSpecifyWhereToGo = AnyController.class.getMethod("didntSpecifyWhereToGo");
final ControllerMethod method = DefaultControllerMethod.instanceFor(AnyController.class, didntSpecifyWhereToGo);
final AnyController controller = new AnyController(validator);
doThrow(new IllegalStateException()).when(messages).assertAbsenceOfErrors();
doThrow(new ValidationFailedException("")).when(messages).assertAbsenceOfErrors();
when(methodInfo.getParametersValues()).thenReturn(new Object[0]);

observer.execute(new InterceptorsExecuted(method, controller));
}

@Test
public void shouldThrowApplicationLogicExceptionIfItsABusinessException() throws Exception {
public void shouldThrowApplicationLogicExceptionIfItsACheckedException() throws Exception {
Method method = AnyController.class.getDeclaredMethod("throwException");
ControllerMethod controllerMethod = instanceFor(AnyController.class, method);
AnyController controller = new AnyController(validator);

expected.expect(ApplicationLogicException.class);
expected.expectCause(any(TestException.class));
expected.expectCause(any(TestCheckedException.class));
observer.execute(new InterceptorsExecuted(controllerMethod, controller));
verify(messages).assertAbsenceOfErrors();
}

@Test
public void shouldThrowTheBusinessExceptionIfItsUnChecked() throws Exception {
Method method = AnyController.class.getDeclaredMethod("throwUnCheckedException");
ControllerMethod controllerMethod = instanceFor(AnyController.class, method);
AnyController controller = new AnyController(validator);

expected.expect(TestException.class);
observer.execute(new InterceptorsExecuted(controllerMethod, controller));
verify(messages).assertAbsenceOfErrors();
}


public static class AnyController {
private final Validator validator;
Expand All @@ -162,10 +176,14 @@ public void didntSpecifyWhereToGo() {
public void specifiedWhereToGo() {
this.validator.onErrorUse(nothing());
}
public void throwException() {
public void throwException() throws Exception {
throw new TestCheckedException();
}
public void throwUnCheckedException() {
throw new TestException();
}
}

private static class TestException extends RuntimeException {}
private static class TestCheckedException extends Exception {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void setUp() {

@Test
public void shouldThrowExceptionIfMessagesHasUnhandledErrors() {
exception.expect(IllegalStateException.class);
exception.expect(ValidationFailedException.class);
exception.expectMessage(containsString("There are validation errors and you forgot to specify where to go."));

messages.add(new SimpleMessage("Test", "Test message"));
Expand Down

0 comments on commit 695c1f4

Please sign in to comment.