Created
June 4, 2014 08:07
-
-
Save rponte/00cde0ab881b694f99a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import static java.util.Collections.list; | |
| import java.sql.SQLException; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import javax.enterprise.context.RequestScoped; | |
| import javax.enterprise.inject.Specializes; | |
| import javax.inject.Inject; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.validation.ConstraintViolation; | |
| import javax.validation.ConstraintViolationException; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import br.com.caelum.vraptor.InterceptionException; | |
| import br.com.caelum.vraptor.Intercepts; | |
| import br.com.caelum.vraptor.Result; | |
| import br.com.caelum.vraptor.controller.ControllerMethod; | |
| import br.com.caelum.vraptor.core.ExceptionMapper; | |
| import br.com.caelum.vraptor.core.InterceptorStack; | |
| import br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor; | |
| import br.com.caelum.vraptor.validator.Message; | |
| import br.com.caelum.vraptor.validator.SimpleMessage; | |
| @Specializes | |
| @RequestScoped | |
| @Intercepts | |
| public class CustomExceptionHandlerInterceptor | |
| extends ExceptionHandlerInterceptor { | |
| private static final Logger logger = LoggerFactory.getLogger(CustomExceptionHandlerInterceptor.class); | |
| private final Result result; | |
| private final HttpServletRequest request; | |
| public CustomExceptionHandlerInterceptor() { | |
| this(null, null, null); | |
| } | |
| @Inject | |
| public CustomExceptionHandlerInterceptor(ExceptionMapper exceptions, Result result, HttpServletRequest request) { | |
| super(exceptions, result); | |
| this.result = result; | |
| this.request = request; | |
| } | |
| public void intercept(InterceptorStack stack, ControllerMethod method, Object resourceInstance) | |
| throws InterceptionException { | |
| try { | |
| stack.next(method, resourceInstance); | |
| } catch (InterceptionException e) { | |
| Throwable root = getRootCause(e); | |
| logger.debug("an error occurs", root); | |
| if (root instanceof SQLException) { | |
| SQLException ex = (SQLException) root; | |
| if (ex.getNextException() == null) { | |
| e = new InterceptionException(ex); | |
| } else { | |
| e = new InterceptionException(ex.getNextException()); | |
| } | |
| } | |
| if (root instanceof ConstraintViolationException) { | |
| logger.info("try to report as JSR303 violation: {}", root); | |
| reportAsJSR303((ConstraintViolationException) root); | |
| } else if (!(e.getCause() instanceof Exception) || !replay((Exception) e.getCause())) { | |
| throw propagate(root); | |
| } | |
| } | |
| } | |
| private void reportAsJSR303(ConstraintViolationException ex) { | |
| List<String> params = list(request.getParameterNames()); | |
| List<Message> errors = new ArrayList<>(); | |
| for (ConstraintViolation<?> item : ex.getConstraintViolations()) { | |
| String category = item.getPropertyPath().toString(); | |
| boolean added = false; | |
| for (String param : params) { | |
| if (param.endsWith(category)) { | |
| errors.add(new SimpleMessage(param, item.getMessage())); | |
| added = true; | |
| continue; | |
| } | |
| } | |
| if (!added) { | |
| errors.add(new SimpleMessage(item.getPropertyPath().toString(), item.getMessage())); | |
| } | |
| } | |
| result.include("errors", errors); | |
| replay(ex); | |
| } | |
| @Override | |
| protected void reportException(Exception e) { | |
| Throwable root = getRootCause(e); | |
| if (!(root instanceof ConstraintViolationException)) { | |
| result.include("exception", root); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment