Saturday, September 20, 2008

how to create a custom fault (exception) in BPEL

As you know while doing Exception handling in BPEL using JDEv when you add a catch block or catch All block you will be given an option to choose the faults. It will list you all the system defined faults like selectionFailure , mismatchedAssignmentFailure , joinFailure , invalidReply , bindingFault and remoteFault. There will be some scenarios where you will have to define application specific exceptions. I will help you on how to get going with exception handling.

For a simple scenario where I need to inform the invoker that I am not Happy create an element and then message type or else create a message type with type string

<element name="faultElement">



<element name="error" type="string"/>




And add message type

<message name=" NotHappyFaultMessage">

<part name="faultcode" element="ns2: faultElement "/>



Just add message Type alone

<message name=" NotHappyFaultMessage">

<part name="faultcode" type="xsd:string"/>


In your Porttype tag while defining the operations specify a fault message with a name

<portType name="MyPortType">

<operation name="getOutput">

<input message="client:Ex1RequestMessage" />

<output message="client:Ex1ResponseMessage"/>

<fault message=" client: NotHappyFaultMessage " name="NotHappy"/>



Once you define this while using catch blocks you will be seeing the custom fault under your wsdl similar to the system faults. There you have your own custom made fault.

If you take a complex scenario you can go for some more categorizations and more complex elements.

For example:- I will define some exceptions my application can throw. Let me categorize them.

1. System exceptions.

2. Application exceptions ß Business exceptions

3. Runtime Exceptions

4. Database Exceptions

You can define your own structure for the exceptions.

For a complex process I will define a basic Fault element as below which is a collection of different other complex types.

<element name="Fault">



<element name="message" type="string"/>

<element name="errorData" type="ErrorData"/>

<element name="exceptionType" type="ExceptionType"/>




<complexType name="ErrorData">


<element name="severity" type="string"/>

<element name="messageId" type="string"/>

<element name="messageParams" type="string"/>



<simpleType name="ExceptionType">

<restriction base="xs:string">

<enumeration value="ApplicationFault"/>

<enumeration value="SystemFault"/>

<enumeration value="RuntimeFault"/>

<enumeration value="BusinessFault"/>

<enumeration value="DatabaseFault"/>



After that we will create a message type for each Fault.

<message name=" ApplicationFaultMessage">

<part name="faultCode" type="xsd: Fault "/>


In your Porttype tag while defining the operations specify a fault message with a name

<fault message="ns1: ApplicationFaultMessage " name="ApplicationFault"/>

Take a look at the Runtime Fault defined by the BPEL to get an idea on how Bpel has defined its own faults.

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

<definitions name="RuntimeFault"




<message name="RuntimeFaultMessage">

<part name="code" type="xsd:string"/>

<part name="summary" type="xsd:string"/>

<part name="detail" type="xsd:string"/>



Hope this helps

No comments: