You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-user@ws.apache.org by Bo...@putnam.com on 2002/08/05 19:59:17 UTC

Custom Exceptions /Fault Listeners

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



Re: Custom Exceptions /Fault Listeners

Posted by Robert Dietrick <rd...@sega.com>.
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:   <ma...@xml.apache.org>
> For additional commands, e-mail: <ma...@xml.apache.org>

--
To unsubscribe, e-mail:   <ma...@xml.apache.org>
For additional commands, e-mail: <ma...@xml.apache.org>


Re: Custom Exceptions /Fault Listeners

Posted by Robert Dietrick <rd...@sega.com>.
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:   <ma...@xml.apache.org>
> For additional commands, e-mail: <ma...@xml.apache.org>

RE: Custom Exceptions /Fault Listeners

Posted by Anne Thomas Manes <an...@manes.net>.
I don't understand why you think that you can't pass your custom exceptions
back through the standard SOAP Fault mechanism. The SOAP Fault mechanism can
propagate any exceptions.

If you don't want to send your excpetions back through the fault mechanism,
then you can set up your service application to catch the exception and send
the exception back as a response message. Then your client application has
to be able to interpret the response message and decipher the exception. If
you let the SOAP fault mechanism handle it, the SOAP runtime will catch the
exception, and return it as a fault, then the SOAP runtime on the client
should rethrow the exception.

Anne

> -----Original Message-----
> From: Boris_Paskalev@putnam.com [mailto:Boris_Paskalev@putnam.com]
> Sent: Monday, August 05, 2002 1:59 PM
> To: soap-user@xml.apache.org
> Subject: Custom Exceptions /Fault Listeners
>
>
> 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:   <ma...@xml.apache.org>
> For additional commands, e-mail: <ma...@xml.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@xml.apache.org>
For additional commands, e-mail: <ma...@xml.apache.org>


RE: Custom Exceptions /Fault Listeners

Posted by Anne Thomas Manes <an...@manes.net>.
I don't understand why you think that you can't pass your custom exceptions
back through the standard SOAP Fault mechanism. The SOAP Fault mechanism can
propagate any exceptions.

If you don't want to send your excpetions back through the fault mechanism,
then you can set up your service application to catch the exception and send
the exception back as a response message. Then your client application has
to be able to interpret the response message and decipher the exception. If
you let the SOAP fault mechanism handle it, the SOAP runtime will catch the
exception, and return it as a fault, then the SOAP runtime on the client
should rethrow the exception.

Anne

> -----Original Message-----
> From: Boris_Paskalev@putnam.com [mailto:Boris_Paskalev@putnam.com]
> Sent: Monday, August 05, 2002 1:59 PM
> To: soap-user@xml.apache.org
> Subject: Custom Exceptions /Fault Listeners
>
>
> 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:   <ma...@xml.apache.org>
> For additional commands, e-mail: <ma...@xml.apache.org>
>