0

I have following code in my backing bean:

try {
    contractService.create(selectedContract);
    conversation.end();
    return "search?faces-redirect=true";
} catch (ActiveContractExistsException e) {
    FacesContext context = FacesContext.getCurrentInstance();
    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, null, e.getMessage()));
    return null;
}

In jsf:

<p:growl id="growl" showDetail="true" showSummary="false" closable="true" autoUpdate="true"/>

and

<p:commandButton value="Save" actionListener="#{contractView.update}" update="growl"/>

As a result I'm getting:

enter image description here

Like you can see this is far from SEVERITY_ERROR. But the most interesting part is when my code is following (adding message outside the catch block):

    try {
        contractService.create(selectedContract);
        conversation.end();
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "Some message"));
        return "search?faces-redirect=true";
    } catch (ActiveContractExistsException e) {
        //FacesContext context = FacesContext.getCurrentInstance();
        //context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "Some message"/*e.getMessage()*/));
        return null;
    }

then all fine:

enter image description here

How can I set SEVERITY_ERROR level to my message while handling exception?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Igor Gorbunov
  • 85
  • 3
  • 12
  • It's hard to understand the last example works. FacesMessages do not survive a PRG (which the `?faces-redirect=true` does). See https://stackoverflow.com/questions/13685633/how-to-show-faces-message-in-the-redirected-page. – Kukeltje Jan 24 '18 at 20:10
  • @Kukeltje Thanks for the responce, I've already solve my problem – Igor Gorbunov Jan 25 '18 at 08:53
  • Possible duplicate of [Handling service layer exception in Java EE frontend method](https://stackoverflow.com/questions/32853167/handling-service-layer-exception-in-java-ee-frontend-method) – Jasper de Vries Jan 27 '18 at 10:17

1 Answers1

0

My problem relates with EE container. In case I'm not extended from EJBException they wrapping automatically, therefore block catch doesn't executing. There are two ways to solve my problem:

  1. Extend my custom exception from EJBException
  2. Add @ApplicationException annotation to my class exception
Igor Gorbunov
  • 85
  • 3
  • 12