0

When loading events for my PF schedule it is sometimes possible that exception occures. Is it posiible to somehow notify the user (using growl component) that such a situation occured?

EDIT:

This is how the exception is caught:

catch (Exception e) {
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Sample error message", "Sample Error"));
    RequestContext.getCurrentInstance().update("scheduleForm:scheduleGrowl");
    e.printStackTrace();
}

And this is the part of the xhtml where the schedule component is defined:

<ui:define name="content">
        <h:form id="scheduleForm">
            <p:growl id="scheduleGrowl" showDetail="true" autoUpdate="true" sticky="true"/> 
            <p:schedule value="#{scheduleController.scheduleModel}"
                view="agendaDay" slotMinutes="15" resizable="false"
                draggable="false" firstHour="8"
                locale="${localeResolver.staticLocale}"
                axisFormat="${localeResolver.defaultTimeFormat}"
                timeFormat="${localeResolver.defaultTimeFormat}"
                >
            </p:schedule>

        </h:form>
    </ui:define>
Wojciech Owczarczyk
  • 5,595
  • 2
  • 33
  • 55

1 Answers1

2

Put the exception-sensitive code in a try-catch and use FacesContext#addMessage() in the catch to add a message to the context.

E.g.

try {
    // ...
} catch (SomeException e) {
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
        FacesMessage.SEVERITY_FATAL, e.getClass().getName(), e.getMessage()));
    // Log the exception as well?
}

If you have however no control over this, then you'd need to create a custom ExceptionHandler. Some hints can be found in this answer: What is the correct way to deal with JSF 2.0 exceptions for AJAXified components? You don't necessarily need to show a full error page (although this is preferable as this kind of exceptions are usually unrecoverable, assuming that this is really no bug/misconfiguration in your code), you could also add a message to the faces context in there.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have thought about that in the first place. I do not know however how to update the or component. I have tried `RequestContext.getCurrentInstance().update("scheduleForm:scheduleGrowl")` but this does not seem to work properly. – Wojciech Owczarczyk Jul 02 '12 at 12:19
  • Set its `autoUpdate` attribute to `true`. Note that it won't be updated if the ajax response is different/malformed due to an uncaught exception. So it really need to be caught and not be thrown through. – BalusC Jul 02 '12 at 12:19
  • Still no luck, exception is caught and not "rethrowned". I have posted code samples above – Wojciech Owczarczyk Jul 02 '12 at 17:51
  • Apparently it's caught at the wrong moment. When exactly is that method invoked? Not during rendering of the view, I presume? That would otherwise explain the behaviour. – BalusC Jul 02 '12 at 19:31
  • Excatly yes. I am ready to take the punishment :). I am quite new to JSF. So why is it happening? – Wojciech Owczarczyk Jul 03 '12 at 10:39
  • Either move the `` to after the ``, or, better, do the initialization job in bean's (post)constructor instead of a getter method. JSF getter methods should be free of business code. – BalusC Jul 03 '12 at 12:42