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">

<complexType>

<sequence>

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

</sequence>

</complexType>

</element>

And add message type

<message name=" NotHappyFaultMessage">

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

</message>

OR

Just add message Type alone

<message name=" NotHappyFaultMessage">

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

</message>

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"/>

</operation>

</portType>

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">

<complexType>

<sequence>

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

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

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

</sequence>

</complexType>

</element>

<complexType name="ErrorData">

<sequence>

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

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

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

</sequence>

</complexType>

<simpleType name="ExceptionType">

<restriction base="xs:string">

<enumeration value="ApplicationFault"/>

<enumeration value="SystemFault"/>

<enumeration value="RuntimeFault"/>

<enumeration value="BusinessFault"/>

<enumeration value="DatabaseFault"/>

</restriction>

</simpleType>

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

<message name=" ApplicationFaultMessage">

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

</message>

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"

targetNamespace="http://schemas.oracle.com/bpel/extension"

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns="http://schemas.xmlsoap.org/wsdl/">

<message name="RuntimeFaultMessage">

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

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

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

</message>

</definitions>

Hope this helps

No comments: