Saturday, February 13, 2016

Displaying a Custom/User friendly message whenever a exception occurs in an ADF Application

Exception handling is very important in any application and there are different ways to do it.

In ADF applications we can handle exceptions in all the three layers of MVC architecture, i.e Model, View and Controller layers.

It’s not just sufficient to handle exceptions but also we have to show users a message they can understand so that they can take necessary actions.

Let’s see how to achieve this in all the three layers:

1) Model Layer

All the exceptions in the Model layer are handled by framework class DCErrorHandlerImpl. We have to create a new class (CustomErrorHandler) which extends DCErrorHandlerImpl and override the methods in it accordingly.

To display a custom message we can either override getDisplayMessage() or reportException() method.

getDisplayMessage() will be called twice everytime an exception occurs. Probably in different phases of ADF lifecycle.

So we can extend reportException() method and pass a custom message in a new JBO Exception as below:

public class customErrorHandler extends DCErrorHandlerImpl {
public void reportException (DCBindingContainer bc, Exception ex) {
       if(you can check some thing here or skip this condition)
            super.reportException(bc,new JboException("My Custom Message"));
        else 
            super.reportException(bc, ex);        
}
}

You have to register this class in Databindings.cpx or adf-config.xml

In Databindings.cpx:

<Application xmlns="http://xmlns.oracle.com/adfm/application" version="12.1.3.10.8" id="DataBindings" SeparateXMLFiles="false" Package="com.oracle.view" ClientType="Generic" ErrorHandlerClass="com.oracle.view.CustomErrorHandler">

In adf-config.xml:

<startup errorHandlerClass="com.oracle.view.CustomErrorHandler">

2) View Layer

The code in managed beans also may throw exceptions. So we have to enclose the code in managed beans by TRY and CATCH blocks.

We can do two things here:

a) Throw a userfriendly faces message in CATCH block.

 FacesContext facesContext = FacesContext.getCurrentInstance();
 facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "My Custom Message", null));

b) Report the Exception to Model layer so that it can be handled in CustomErrorHandler class again.

((DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry()).reportException(new JboException("My Custom Message")); 

3) Controller Layer

If something goes wrong in Taskflows it can be handled in a mehod activity marked as Exception handler.

To do this drag and drop a method activity in to the Taskflow and mark it as a exception handler by clicking the red color '!' sign at the top of the taskflow .

 
In this method code we can throw a userfriendly faces message on to the screen.

 FacesContext facesContext = FacesContext.getCurrentInstance();
 facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "My Custom Message", null));


No comments:

Post a Comment