How to customize the XML error message?

J

Jaime Stuardo

Hi all....

When I explicitly throw an exception from my Web Service, this XML is sent:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>System.Web.Services.Protocols.SoapException:
Server was unable to process request. --->
System.Data.SqlClient.SqlException: El beneficiario requerido no existe en el
sistema de VidaSecurity
at beneficiario.Ejecuta(String store_proc, Boolean aprobacion)
at beneficiario_ObtienePago()
at convenio.Consulta(QueryType datos)
--- End of inner exception stack trace ---</faultstring><detail
/></soap:Fault></soap:Body></soap:Envelope>

As you see, faultstring tag contains very technical information that is not
useful for the user. Is it possible to change the level of error message so
that only my customized message is shown in faultstring tag?

Thanks
Jaime
 
A

Andrew Backer

Aren't these type of exceptions exactly what is supposed to be thrown?
How would you have .net put something more friendly in an unhandled
exception? You are just going to have to trap likely exceptions (as
you should be doing with security exceptions, right?), and then throw a
custom SoapException that your app knows how to respond to. There may
be a more elegant way to handle error codes in soap, but it seems like
using custom soapExceptions was the easiest way in .net

The only thing I ran into is the fact that the error code can not be
entirely numeric, so "e001" was necessary. There are a few other fun
things about parsing one of the fields, but just throw one that you
make and check out every field of the returned exception for what you
need.
 
M

Michael Nemtsev

Hello Jaime,

You can customize you error by using XmlQualifiedName

and includind this instance to the SoapException

JS> When I explicitly throw an exception from my Web Service, this XML
JS> is sent:
JS>
JS> As you see, faultstring tag contains very technical information that
JS> is not useful for the user. Is it possible to change the level of
JS> error message so that only my customized message is shown in
JS> faultstring tag?
 
A

Andrew Backer

In more detail, here is ~approx what I do.

On the server I throw this type of exception (i have a wrapper class
that derives from soapException, and helpers) :

throw new SoapException( "Mesage Here", new XmlQualifiedName("ERR_001",
"App/Server/Excep"));

On the client I do something like this to grab and extract the error :
try {
call WS
} catch ( SoapException ex ) {
switch ( ex.Code.Name.Trim() ) {
case "ERR_001": // do something
....

Hope this helps. I use more descriptive names that ERR_001 too. There
may be funner ways to do this, but I'm new at WS so i'm not sure. I
would like to catch a subclassed SoapException actually, but not sure
If I can do that.

- Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top