Skip to content

Commit

Permalink
Merge pull request #64 from caelum/wrapping_exceptions
Browse files Browse the repository at this point in the history
Unwrapping exceptions for JSP files too
  • Loading branch information
Turini committed Sep 13, 2013
2 parents cd46a4b + 0c3654f commit 6bb2edc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import br.com.caelum.vraptor4.http.EncodingHandler;
import br.com.caelum.vraptor4.http.VRaptorRequest;
import br.com.caelum.vraptor4.http.VRaptorResponse;
import br.com.caelum.vraptor4.interceptor.ControllerInvocationException;
import br.com.caelum.vraptor4.interceptor.ApplicationLogicException;
import br.com.caelum.vraptor4.ioc.Container;
import br.com.caelum.vraptor4.ioc.ContainerProvider;

Expand Down Expand Up @@ -110,10 +110,10 @@ public Object insideRequest(Container container) {

try {
provider.provideForRequest(request, execution);
} catch (ControllerInvocationException e) {
} catch (ApplicationLogicException e) {
// it is a business logic exception, we dont need to show
// all interceptors stack trace
throw new ServletException("Your controller threw an exception", e.getCause());
throw new ServletException(e.getMessage(), e.getCause());
}

logger.debug("VRaptor ended the request");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package br.com.caelum.vraptor4.interceptor;

import br.com.caelum.vraptor4.InterceptionException;

/**
* When a controller or JSP throws an exception, we use this one to wrap it, so
* we can unwrap after it leaves the interceptor stack
*
*/
@SuppressWarnings("serial")
public class ApplicationLogicException extends InterceptionException {

public ApplicationLogicException(String msg) {
super(msg);
}

public ApplicationLogicException(String msg, Throwable e) {
super(msg, e);
}

public ApplicationLogicException(Throwable e) {
super(e);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void intercept(InterceptorStack stack, ControllerMethod method, Object co
// fine... already parsed
log.trace("swallowing {}", cause);
} else {
throw new ControllerInvocationException("exception raised, check root cause for details: " + cause, cause);
throw new ApplicationLogicException("your controller raised an exception", cause);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private Object invokeMethod(Object interceptor, Method stepMethod,
return returnObject;
} catch (MirrorException e) {
// we dont wanna wrap it if it is a simple controller business logic exception
Throwables.propagateIfInstanceOf(e.getCause(), ControllerInvocationException.class);
Throwables.propagateIfInstanceOf(e.getCause(), ApplicationLogicException.class);
throw new InterceptionException(e.getCause());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import br.com.caelum.vraptor4.core.MethodInfo;
import br.com.caelum.vraptor4.http.MutableRequest;
import br.com.caelum.vraptor4.http.MutableResponse;
import br.com.caelum.vraptor4.interceptor.ApplicationLogicException;
import br.com.caelum.vraptor4.proxy.MethodInvocation;
import br.com.caelum.vraptor4.proxy.Proxifier;
import br.com.caelum.vraptor4.proxy.ProxyInvocationException;
Expand Down Expand Up @@ -62,12 +63,12 @@ public DefaultPageResult(MutableRequest req, MutableResponse res, MethodInfo req
}

public void defaultView() {
String to = resolver.pathFor(requestInfo.getControllerMethod());
logger.debug("forwarding to {}", to);
try {
String to = resolver.pathFor(requestInfo.getControllerMethod());
logger.debug("forwarding to {}", to);
request.getRequestDispatcher(to).forward(request.getOriginalRequest(), response.getOriginalResponse());
} catch (ServletException e) {
throw new ResultException(e);
throw new ApplicationLogicException(to + " raised an exception", e);
} catch (IOException e) {
throw new ResultException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import br.com.caelum.vraptor4.core.MethodInfo;
import br.com.caelum.vraptor4.http.MutableRequest;
import br.com.caelum.vraptor4.http.MutableResponse;
import br.com.caelum.vraptor4.interceptor.ApplicationLogicException;
import br.com.caelum.vraptor4.proxy.JavassistProxifier;
import br.com.caelum.vraptor4.proxy.ObjenesisInstanceCreator;
import br.com.caelum.vraptor4.proxy.Proxifier;
Expand Down Expand Up @@ -136,7 +137,7 @@ public void shouldAllowCustomPathResolverWhileForwardingView() throws ServletExc
verify(dispatcher, only()).forward(request, response);
}

@Test(expected=ResultException.class)
@Test(expected=ApplicationLogicException.class)
public void shouldThrowResultExceptionIfServletExceptionOccursWhileForwardingView() throws Exception {
when(request.getRequestDispatcher(anyString())).thenReturn(dispatcher);
doThrow(new ServletException()).when(dispatcher).forward(request, response);
Expand Down

0 comments on commit 6bb2edc

Please sign in to comment.