Web services use SOAP faults to report fault cases back to clients. The faults can be generated from the SOAP framework in a case of invalid SOAP messages, invalid security tokens or they can be generated from the service business logic itself. The fault messages may contain simply a string indicating the error, or it may contain lot of details which could be useful to the clients find the problem. In fact the format of the SOAP fault is a standard. But services can send custom details within the details element in a SOAP fault.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Sender</faultcode>
<faultstring>..</faultstring>
<detail> You Custom Message </detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
And you can define the schema of your custom fault message in a WSDL, so the clients can prepare to handle fault scenarios. In facts tools like Apache Axis2 WSDL2C, WSDL2Java tool will help you to work with custom faults when they are defined in the WSDL. Here is an example section of a WSDL with an operation which can throw two faults “MyFirstException”, “MySecondException”.
<!-- fault schemas -->
<types>
<schema ..>
<element name="MyFirstException">
<complexType>
<sequence>
<element name="text" type="xsd:string"/>
</sequence>
</complexType>
</element>
<!-- fault element -->
<element name="MySecondException">
<complexType>
<sequence>
<element name="number" type="xsd:int"/>
</sequence>
</complexType>
</element>
</schema>
</types>
<!-- the fault messages -->
<message name="MySecondExceptionFault">
<part name="fault" element="ns1:MySecondException"/>
</message>
<message name="MyFirstExceptionFault">
<part name="fault" element="ns1:MyFirstException"/>
</message>
<!-- operation to throw fault -->
<portType name="MyType">
<operation name="myOperation">
<input message="tns:myOperationRequest"/>
<output message="tns:myOperationResponse"/>
<fault name="MySecondException" message="tns:MySecondExceptionFault"/>
<fault name="MyFirstException" message="tns:MyFirstExceptionFault"/>
</operation>
</portType>
Note that the operation “myOperation” is throwing faults “MyFirstException”, “MySecondExcpetion”. If you generate the Java code for this operation, it would be simple as this,
public MyOperationRequest
MyOperation(MyOperationRequest) throws
MyFirstExcpetion,
MySecondExcpetion {
// here is your business logic
}
Anyway we are going to write codes in ‘C’ language, which doesn’t have similar exception mechanism. Let see how we can do it, starting with writing the service and then writing a client.
Writing Services With Custom SOAP Faults
Once you generate the ‘C’ codes for the WSDL using WSDL2C tool, you should first have a look at the skeleton header file.
/**
* the generated fault union for operation "myOperation|urn:myuri:1.0",
* in a case you want to return a fault, put the appropriate adb object for
* the union variable pointer comes as the last parameter of the method
*/
typedef union
{
adb_MyFirstException_t* MyFirstException;
adb_MySecondException_t* MySecondException;
} axis2_skel_MyService_myOperation_fault;
/**
* auto generated function declaration
* for "myOperation|urn:myuri:1.0" operation.
* @param env environment ( mandatory)
* @param _myOperation of the adb_myOperation_t*
*
* @return adb_myOperationResponse_t*
*/
adb_myOperationResponse_t* axis2_skel_MyService_myOperation(const axutil_env_t *env,
adb_myOperation_t* _myOperation,
axis2_skel_MyService_myOperation_fault *fault);
And at the very end of the header file, you will see an enumeration of constants corresponding to each fault is generated.
typedef enum
{
AXIS2_SKEL_MYSERVICE_ERROR_NONE = AXIS2_SKEL_MYSERVICE_ERROR_CODES_START,
AXIS2_SKEL_MYSERVICE_MYOPERATION_FAULT_MYFIRSTEXCEPTION,
AXIS2_SKEL_MYSERVICE_MYOPERATION_FAULT_MYSECONDEXCEPTION,
AXIS2_SKEL_MYSERVICE_ERROR_LAST
} axis2_skel_MyService_error_codes;
That’s all you need to aware of. The plan is whenever you need to report the fault, you have to do three things inside the business logic.
- Create the adb object for the fault, in this case either “adb_MyFirstException_t” or “adb_MySecondException_t” and set it to the fault pointer variable.
- Set the constant corresponding to the fault using “AXIS2_ERROR_SET” function
- return NULL
Here is an example how you do it inside the actual business logic code.
adb_myOperationResponse_t* axis2_skel_MyService_myOperation(const axutil_env_t *env,
adb_myOperation_t* myOperation,
axis2_skel_MyService_myOperation_fault *fault )
{
/* the buisness logic */
....
if(/* checking some condition to throw the "MyFirstException" fault */)
{
/* 1. Creating the adb object and set it to the fault pointer variable */
adb_MyFirstException_t *exp = NULL;
exp = adb_MyFirstException_create(env);
adb_MyFirstException_set_text(exp, env, "this is the exception 1"); /* custom value */
fault->MyFirstException = exp;
/* 2. Setting the error constant corrosponding to the fault */
AXIS2_ERROR_SET(env->error,
AXIS2_SKEL_MYSERVICE_MYOPERATION_FAULT_MYFIRSTEXCEPTION,
AXIS2_FAILURE);
/* 3. Returning NULL */
return NULL;
}
else if(/* checking some condition to throw the "MySecondException" fault */)
{
/* 1. Creating the adb object and set it to the fault pointer variable */
adb_MySecondException_t *exp = NULL;
exp = adb_MySecondException_create(env);
adb_MySecondException_set_number(exp, env, 2);/* custom value */
fault->MySecondException = exp;
/* 2. Setting the error constant corrosponding to the fault */
AXIS2_ERROR_SET(env->error,
AXIS2_SKEL_MYSERVICE_MYOPERATION_FAULT_MYSECONDEXCEPTION,
AXIS2_FAILURE);
/* 3. Returning NULL */
return NULL;
}
/* return the response in no fault scenario */
return response;
}
That’s all you have to do, Axis2/C will make sure to build the fault and put your custom message inside the details element.
Writing Clients to Handle custom SOAP Faults
After generating the code for clients using WSDL2C tool, this time you should look at the generated stub header file first. It is just similar to the skeleton header files may be except all the “skel” prefixes are renamed to “stub” and additional parameter “stub” for the operation.
/**
* the generated fault union for operation "myOperation|urn:myuri:1.0",
* in a case the server return a fault, the corresponding adb object will be loaded for
* the union variable pointer comes as the last parameter of the method
*/
typedef union
{
adb_MyFirstException_t* MyFirstException;
adb_MySecondException_t* MySecondException;
} axis2_stub_MyService_myOperation_fault;
/**
* auto generated function declaration
* for "myOperation|urn:myuri:1.0" operation.
* @param env environment ( mandatory)
* @param _myOperation of the adb_myOperation_t*
*
* @return adb_myOperationResponse_t*
*/
adb_myOperationResponse_t* axis2_stub_MyService_myOperation(axis2_stub_t* stub, const axutil_env_t *env,
adb_myOperation_t* _myOperation,
axis2_stub_MyService_myOperation_fault *fault);
typedef enum
{
AXIS2_STUB_MYSERVICE_ERROR_NONE = AXIS2_STUB_MYSERVICE_ERROR_CODES_START,
AXIS2_STUB_MYSERVICE_MYOPERATION_FAULT_MYFIRSTEXCEPTION,
AXIS2_STUB_MYSERVICE_MYOPERATION_FAULT_MYSECONDEXCEPTION,
AXIS2_STUB_MYSERVICE_ERROR_LAST
} axis2_stub_MyService_error_codes;
Looking at this, you may have got the idea how to differentiate what fault is being thrown by the server and how to extract the parameters of the custom fault. Here is an example client code correctly handling exceptions.
/* the structure to keep the fault */
axis2_stub_MyService_myOperation_fault fault;
..... /* the part preparing the request is ignored here */
/* invoking the "myOperation" operation */
response = axis2_stub_op_MyService_myOperation(stub, env, op, &fault);
/* checking the response == NULL implies fault is sent */
if(response == NULL)
{
/* getting the error number to distinguish the fault */
error_code = env->error->error_number;
/* compare error code with constants of each faults */
if(error_code == AXIS2_STUB_MYSERVICE_MYOPERATION_FAULT_MYFIRSTEXCEPTION) {
/* extracting out the adb objects */
axis2_char_t *text = adb_MyFirstException_get_text(fault.MyFirstException, env);
/* do a printf of the message */
printf("My First Exception called: with param %s\\n", text);
}
else if(error_code == AXIS2_STUB_MYSERVICE_MYOPERATION_FAULT_MYSECONDEXCEPTION) {
/* extracting out the adb objects */
int number = adb_MySecondException_get_number(fault.MySecondException, env);
/* do a printf of the message */
printf("My Second Exception called: with param %d\\n", number);
}
}
Note that this feature is available only in the very latest WSDL2C tool. Try to get latest build from Axis2/Java to use this up to date tool.
You can download the WSDL and codes used in this example from here, https://issues.apache.org/jira/secure/attachment/12399724/case45.zip