ws-soap-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Dietrick <rdietr...@sega.com>
Subject Re: Custom Exceptions /Fault Listeners
Date Mon, 05 Aug 2002 18:44:32 GMT
You can return additional information (beyond faultCode and faultString)
in the Fault by writing a custom SOAPFaultListener and configuring your
application to use it in your deployment descriptor.

Look at the javadocs for the Fault class, and you'll see that there is a
method called setDetailEntries(), which can be 
used to set information (in the form of XML structures) to be passed
back to the client.  In addition to the SOAPFaultListener on the server,
you'll need an analogous class to parse the Fault for the new
information on the client.

There's a good example of this in the O'Reilly title Java & SOAP by
Robert Englander.

For what it's worth, here are the relevant pieces of the code I wrote to
pass back an errorCode attribute, which gives detailed info about what
exactly the problem was:

SOAPFaultListener implementation:
	        CustomSOAPException se =
(CustomSOAPException)faultEvent.getSOAPException();
		Vector details = new Vector();
		DocumentBuilder builder = XMLParserUtils.getXMLDocBuilder();
		Document doc = builder.newDocument();
		Element elem = doc.createElement("ERROR_CODE");
		Text txt = doc.createTextNode("");
		txt.setData(String.valueOf(se.getErrorCode()));  // getErrorCode() is
part of my CustomSOAPException class
		elem.appendChild(txt);
		details.addElement(elem);
		faultEvent.getFault().setDetailEntries(details);

FaultParser, used on the client to construct an identical
CustomSOAPException as was rendered on the server:

	// get the detail entries from the Fault
	Vector entries = fault.getDetailEntries();
	int cnt = entries.size();
	// default to UNKNOWN error code
	int errorCode = MyConstants.ERR_UNKNOWN;
	for (int i=0;i<cnt;i++) {
	    Element elem = (Element)entries.elementAt(i);
	    Node node = elem.getFirstChild();
	    String attrName = elem.getNodeName();
	    String attrValue = node.getNodeValue();
	    // look for an ERROR_CODE value in the detail entries
	    if (attrName.equals("ERROR_CODE")) {
		errorCode = Integer.parseInt(attrValue);
	    }
	}
	CustomSOAPException se = new CustomSOAPException(errorCode,
fault.getFaultString());
	// set the errorType (CLIENT/SERVER)
	String fCode = fault.getFaultCode();
	if (fCode.equals(Constants.FAULT_CODE_CLIENT)) {
	    se.setErrorType(MyConstants.CLIENT_ERROR);
	}
	else if (fCode.equals(Constants.FAULT_CODE_SERVER)) {
	    se.setErrorType(MyConstants.SERVER_ERROR);
	}
	return se;
    }

and the relevant line from the deployment descriptor:
 
<isd:faultListener>com.mypackage.CustomFaultListener</isd:faultListener>

Hope this helps.

-rob

Boris_Paskalev@putnam.com wrote:
> 
> Hi all,
> I'm trying to get across SOAP a custom Exceptions (instead of Faults) Is
> there are an example or simple way of
> catching a custom Exception (say MyException)  on the clients side, after
> it was thrown form the SOAP service.
> 
> I need to distinguish between exception in order to produce according
> actions to the exception. For example if I have a missing file exception I
> can
> go ahead and call a service that creates this file)
> 
> If someone has an idea how to do this or know of an example I would
> appreciate it.
> 
> Thanks,
> Boris Paskalev
> 
> --
> To unsubscribe, e-mail:   <mailto:soap-user-unsubscribe@xml.apache.org>
> For additional commands, e-mail: <mailto:soap-user-help@xml.apache.org>

Mime
View raw message