Home » ADF » ADF: Custom Error Handler to Display Custom Message to User
ADF: Custom Error Handler to Display Custom Message to User

ADF: Custom Error Handler to Display Custom Message to User

There’s another post which describes doing the same thing in another way.
Check out here

Sometimes user wants to display custom error message instead of default message. For such cases ADF framework provides a way to create custom error handler class which extends default DCErrorHandlerImpl class. You are not required to write any code to register your custom exception handler class. Instead, you’ve to select the root node of the DataBindings.cpx files in the Structure window, and then use the Property Inspector to set the ErrorHandlerClass property to the fully qualified name of the error handler you want to use.

In this example I’ll give the custom message for validation exception when more than one row is using same primary key. Default message displayed by ADF framework is-

JBO-25013 Too many object match primary key. And  I’ll replace it with custom message. Hear I’m using Hr schema Employees table.

 

Step1: Create Entity Object and View Object using Employees table.

Step2: Drag-drop this view object in jspx page as ADF table.

Step3: Create java class with which extends DCErrorHandlerImpl class.

 

 

package com.in.adftutorials.view;

import oracle.adf.model.binding.DCBindingContainer;

import oracle.adf.model.binding.DCErrorHandlerImpl;

import java.sql.SQLException;

import oracle.adf.model.BindingContext;

import oracle.adf.model.binding.DCBindingContainer;

import oracle.adf.model.binding.DCErrorHandlerImpl;

import oracle.jbo.JboException;

 

 

public class MyCustomErrorHandler extends DCErrorHandlerImpl {

public MyCustomErrorHandler() {

this(true);

}

 

public MyCustomErrorHandler(boolean setToThrow) {

super(setToThrow);

}

 

@Override

public void reportException(DCBindingContainer dCBindingContainer,

Exception exception) {

super.reportException(dCBindingContainer, exception);

}

 

 

public String getDisplayMessage(BindingContext ctx, Exception ex) {

String message="";

if (ex instanceof oracle.jbo.ValidationException) {

 

String msg = ex.getMessage();

int i=msg.indexOf("JBO-25013");//When JBO-25013 Too many object match promary key exception occur.

if(i>0)

{

message= "Duplicate Employee Id Found.";

}

message= getDisplayMessage(ctx,ex);

}

else

{

message=getDisplayMessage(ctx,ex);

}

return message;

}

}

 

getDisplayMessage(): Returns the message that will be reported to JSF for

each error that occurs. I have overridden this method and provided a custom message when JBO-25013 exception occurs, in rest of all cases it’ll give the default message.

 

 

Step4: Register MyCustomErrorHandler class into Databinding.cpx file.

 

<?xml version="1.0" encoding="UTF-8" ?>

<Application xmlns="http://xmlns.oracle.com/adfm/application"

version="11.1.1.60.13" id="DataBindings" SeparateXMLFiles="false"

Package="com.in.adftutorials.view" ClientType="Generic"

ErrorHandlerClass="com.in.adftutorials.view.MyCustomErrorHandler">

<pageMap>

<page path="/test.jspx" usageId="com_in_adftutorials_view_testPageDef"/>

</pageMap>

<pageDefinitionUsages>

<page id="com_in_adftutorials_view_testPageDef"

path="com.in.adftutorials.view.pageDefs.testPageDef"/>

</pageDefinitionUsages>

<dataControlUsages>

<BC4JDataControl id="AppModuleDataControl"

Package="com.in.adftutorials.model"

FactoryClass="oracle.adf.model.bc4j.DataControlFactoryImpl"

SupportsTransactions="true" SupportsFindMode="true"

SupportsRangesize="true" SupportsResetState="true"

SupportsSortCollection="true"

Configuration="AppModuleLocal" syncMode="Immediate"

xmlns="http://xmlns.oracle.com/adfm/datacontrol"/>

</dataControlUsages>

</Application>

 

Running application without registering custom error handler class.

4-05-1

 

Output when registering MyCustomErrorHandler class to databinding .cpx file.

4-05-2

Sample Code

About Manish Pandey

Manish Pandey is a Senior Developer with more than 3 years of experience in JAVA Programming. Currently enhancing skills in ADF.

8 comments

  1. very useful.Its save my time .

  2. thank you!!!!

    thank you
    thank you
    thank you!!!

  3. i am getting the following error. I followed the above steps given by you. Could u plz tell me anything elz i need to configure ??

    ADF_FACES-60098:Faces lifecycle receives unhandled exceptions in phase RENDER_RESPONSE 6
    java.lang.StackOverflowError
    at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(SingleByteEncoder.java:91)
    at sun.nio.cs.SingleByteEncoder.encodeLoop(SingleByteEncoder.java:130)
    at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:544)
    at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:252)
    at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:106)
    at java.io.OutputStreamWriter.write(OutputStreamWriter.java:190)
    at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
    at java.io.PrintStream.write(PrintStream.java:476)
    at java.io.PrintStream.print(PrintStream.java:619)
    at java.io.PrintStream.println(PrintStream.java:756)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:54)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)
    at view.MyCustomErrorHandler.getDisplayMessage(MyCustomErrorHandler.java:72)

  4. First of all thx you for your example but I want to ask about all errors that can happend haw I can handle it not one error or one exception haw I can allocate all errors and it’s meaning and handle it by one solution can you help me in that by any idea

    • Alaa,
      you don’t worry about other error that are handle automatically and given default message to user.
      you just handle those error whom you want to give custom Message.

  5. I would like to have an error handler per page in my application. For instance, if duplicate record error is to be displayed for two pages (say employee and department) how can I differentiate in this error handler as to from which page this error comes from. I need this info to formulate my error message as Duplicate employee found or Duplicate department found. The handler is registered to the Databindings.cpx and hence becomes common for all pages. How can make a page specific error handler.

  6. Why I cannot use this approach for Webcenter Portal exception?

  7. This post helped me a lot. Thanks! Though the code above this page and the downloaded sample project’s code was slightly different. I got StackOverflow error first when I copied the code on this page.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>