471,108 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,108 software developers and data experts.

Throwing Errors in Web Service

I was wondering how to handle an error in a web service. If our web service
encounters a connection error or any other error what is the best practice
for returning an error? Do you return a blank result with an error attached
or is there some other way to do this?

thanks,
Anthony
Aug 23 '06 #1
2 1783
>I was wondering how to handle an error in a web service. If our web
>service
encounters a connection error or any other error what is the best practice
for returning an error? Do you return a blank result with an error
attached
or is there some other way to do this?

SOAPFault is just exactly for the same.

--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.org
http://www.edujini.in | http://webservices.edujini.in
-------------------
Aug 23 '06 #2
What I do is trap the exception within the Web Service and then throw
and then throw it as a SoapException.

In Web Service:
Try
' test for some condition and treat it like a system
exception
If Something_Bad_Happened() Then
Throw New System.Exception("Something Bad Happened
Exception")
End If

Catch ex As System.Exception

' certain types of exceptions you might re-throw as a
SoapExceptions
' other types you might ignore or handle on the web service
side.
If ex.Message = "Something Bad Happened Exception" Then
' Build the detail element of the SOAP fault.
Dim doc As New System.Xml.XmlDocument
Dim node As System.Xml.XmlNode = _
doc.CreateNode(XmlNodeType.Element, _
SoapException.DetailElementName.Name, _
SoapException.DetailElementName.Namespace)

' Build specific details for the SoapException.
' Add details as the first child to the XML element
that is already created.
Dim details As System.Xml.XmlNode = _
doc.CreateNode(XmlNodeType.Element, _
"mySpecialInfo1", "http://tempuri.org/")

' Add details2 as the first child to the XML element
that is already created.
Dim details2 As System.Xml.XmlNode = _
doc.CreateNode(XmlNodeType.Element, _
"mySpecialInfo2", "http://tempuri.org/")
Dim attr As XmlAttribute = doc.CreateAttribute("t", _
"attrName", "http://tempuri.org/")
attr.Value = "attrValue"
details2.Attributes.Append(attr)

' Append the two child elements to the detail node.
node.AppendChild(details)
node.AppendChild(details2)

'Throw SOAP exception to be returned by Web Service
Dim se As New SoapException("The Web Service Failed
Because Something Really Bad Happened.", _
SoapException.ServerFaultCode, _
HttpContext.Current.Request.Url.AbsoluteUri, node)
Throw se
Else
Throw ex
End If
End Try

On Client Side:

Try
WS.MyWebServiceMethodCall()
Catch Se As System.Web.Services.Protocols.SoapException
MsgBox(Se.Message,
MsgBoxStyle.Critical, "Soap Exception in
thrown in Web Service")
Catch ex As System.Exception
MsgBox(ex.Message, _
MsgBoxStyle.Critical, "Exception in
Client")
End Try

This way you can distinguish between exceptions caused by the Web
Service and exceptions thrown in the client.

The error thrown by the Web Service may have originally been an
exception or just some error condition that you decide to throw as a
SoapException.

You don't really need to worry about the Web Service parameters since
once the Soap Exception is thrown they are no longer relevant.

If the exception is originally thrown by the web service, with a little
more work, you can throw it as a SoapException with the inner exception
containing the original message string.

I'm not sure if this is the best practice, but if you hunt around on
msdn.com I think there are some other examples of how to use
SoapException. I just threw this together so it's a bit sloppy, but
you get the idea...

Good Luck,
-Paul
Anthony Biondo Jr wrote:
I was wondering how to handle an error in a web service. If our web service
encounters a connection error or any other error what is the best practice
for returning an error? Do you return a blank result with an error attached
or is there some other way to do this?

thanks,
Anthony
Aug 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Russ | last post: by
40 posts views Thread by Kevin Yu | last post: by
3 posts views Thread by Joel D. Kraft | last post: by
21 posts views Thread by Chris M. Thomasson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.