Capture through the Exceptionhandler

Asked

Viewed 162 times

3

I’m trying to capture a javax.validation.ValidationException in my MB to later play an alert. I tried with the three examples below independent but nothing returned. Should I enter any more code ? I use version 2.5.0.

@ViewController
public class MeuMB extends AbstractEditPageBean<CTe, Long> {

@ExceptionHandler
public void tratar(RuntimeException ex) {
    System.out.println("1111" + ex);
}

@ExceptionHandler
public void tratar2(ValidationException ex) {
    System.out.println("2222" + ex);
}

@ExceptionHandler
public void tratar3(Exception ex) {
    System.out.println("3333" + ex);
}

The class has @Viewcontroller and it already calls @Controller.

My calling method is as follows::

public String upload() {
    //int i = 5 / 0;

    try {
        this.gera();
    } catch (Exception ex) {  
        getMessageContext().add(getResourceBundle().getString("cte.msg.uploadFail"), SeverityType.ERROR);
        return null;
    }

If I take the comment from "5 / 0", error occurs and it is called the Runtimeexception. So the calls and Imports would be ok.

Problem is that in the "generate" method I force null into a field not null. Only to test.

@NotNull
@JoinColumn(name = "municipio_id", referencedColumnName = "id")
@ManyToOne(optional = false)
private Municipio municipio;

And this mistake I am not able to capture neither in Try catch nor by calls Exceptionhandler.

2 answers

2

The validation triggers a ConstraintViolationException which can be encapsulated in TransactionException.

So I opted for the following solution:

@ExceptionHandler
public void tratarErroInesperado(RuntimeException e) throws IOException {
    if (tratarConstraintViolationException(e)) {
        return;
    } else {
        ...
    }
}

private boolean tratarConstraintViolationException(Exception e) {
    Throwable cause = e;
    while (cause != null) {
        if (cause instanceof ConstraintViolationException) {
            for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
                messageContext.add(violation.getMessage(), SeverityType.ERROR);
            }
            return true;
        }
        cause = cause.equals(cause.getCause()) ? null : cause.getCause();
    }
    return false;
}

1

According to the documentation, these methods will only be invoked if the exception occurs in some other method, in this case the Meumb class, to test it is necessary to create another method and cause the exception.

@Controller
public class Simples {

    @ExceptionHandler
    public void tratador(NullPointerException cause) { }

    @ExceptionHandler
    public void tratador(AbacaxiException cause) { }    

    public void inserir() { }   

    public void alterar() { }   

    public void excluir() { }   
}

If Nullpointerexception or Pineapple exceptions occur in Simple class methods, treatment will be delegated to your keeper.

http://demoiselle.sourceforge.net/docs/framework/reference/2.5.0-RC1/html_single/#d0e1029

Browser other questions tagged

You are not signed in. Login or sign up in order to post.