[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.webservices

How to handle common/routine exceptions?

(IanT)

7/22/2004 2:06:00 AM

There seems to be common agreement that all Web Service exceptions
should be passed to the client as a SoapException. I do this but even
when raising a custom soap exception the throw command returns
information about private methods/line numbers etc.

While this is OK for unhandled exceptions it seems messy for errors
such as "User not logged in.". Is there a way of throwing a
SoapException which doesn't provide this additional information to the
client? Maybe i'm being petty but i can't imagine getting a login
failure message from a commercial microsoft web service that gives me
the line number the error was thrown from.

Is this how other developers are handling these exceptions? If it's
the best practice i have no real problem with it - it just doesn't
seem quite right.

any comments appreciated.

Ian

--Error Message being returned--
System.Web.Services.Protocols.SoapException: Not logged in.
at SSRAUploadWebService.RAService.CheckLoggedIn() in
c:\inetpub\wwwroot\SSRAUpload\RAService.asmx.vb:line 81
at SSRAUploadWebService.RAService.UploadBatch(XmlDocument XMLDoc)
in c:\inetpub\wwwroot\SSRAUpload\RAService.asmx.vb:line 111"

--Code used to throw Exception--

Private Function CheckLoggedIn() As Boolean
If (sessionLoginSecurity Is Nothing) OrElse
sessionLoginSecurity.IsLoggedIn = False Then
Throw RaiseException("checkLoggedIn", "Not logged in.",
errorCodes.NotLoggedIn, "", FaultCode.Client)
End If

End Function

Public Function RaiseException(ByVal uri As String, _
ByVal errorMessage As String, _
ByVal errorNumber As errorCodes, _
ByVal errorSource As String, _
ByVal code As FaultCode) As SoapException

Dim webServiceNamespace As String =
"http://flexirent.com/webServices/SSRAUpload/RAService....
Dim faultCodeLocation As XmlQualifiedName
'Identify the location of the FaultCode
Select Case code
Case FaultCode.Client
faultCodeLocation = SoapException.ClientFaultCode

Case FaultCode.Server
faultCodeLocation = SoapException.ServerFaultCode
End Select


Dim xmlDoc As XmlDocument = New XmlDocument

'Create the Detail node
Dim rootNode As XmlNode =
xmlDoc.CreateNode(XmlNodeType.Element, _

SoapException.DetailElementName.Name, _

SoapException.DetailElementName.Namespace)

'Build specific details for the SoapException
'Add first child of detail XML element.
Dim errorNode As XmlNode =
xmlDoc.CreateNode(XmlNodeType.Element, "Error", webServiceNamespace)

'Create and set the value for the ErrorNumber node
'Dim errorNumberNode As XmlNode =
xmlDoc.CreateNode(XmlNodeType.Element, "ErrorNumber",
webServiceNamespace)
'errorNumberNode.InnerText = errorNumber

'Create and set the value for the ErrorMessage node
Dim errorMessageNode As XmlNode =
xmlDoc.CreateNode(XmlNodeType.Element, "ErrorMessage",
webServiceNamespace)
errorMessageNode.InnerText = errorMessage

'Create and set the value for the ErrorSource node
Dim errorSourceNode As XmlNode =
xmlDoc.CreateNode(XmlNodeType.Element, "ErrorSource",
webServiceNamespace)
errorSourceNode.InnerText = errorSource

'Append the Error child element nodes to the root detail node.
'errorNode.AppendChild(errorNumberNode)
errorNode.AppendChild(errorMessageNode)
errorNode.AppendChild(errorSourceNode)

'Append the Detail node to the root node
rootNode.AppendChild(errorNode)

'Construct the exception
Dim soapEx As SoapException = New SoapException(errorMessage,
faultCodeLocation, uri, rootNode)

'Raise the exception back to the caller
Return soapEx

End Function
1 Answer

Jan Tielens

6/9/2004 7:20:00 AM

0

You may want to read following articles:
http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/soapexcep...
http://msdn.microsoft.com/library/en-us/dnservice/html/service09...

The last one explains how to hide the line numbers etc.

--
Greetz
Jan
________________
Read my weblog: http://weblogs.a...


"IanT" <itinsleyTAKETHISOUT@hotmail.com> schreef in bericht
news:1f5c7ec3.0406082104.1e1ca8ba@posting.google.com...
> There seems to be common agreement that all Web Service exceptions
> should be passed to the client as a SoapException. I do this but even
> when raising a custom soap exception the throw command returns
> information about private methods/line numbers etc.
>
> While this is OK for unhandled exceptions it seems messy for errors
> such as "User not logged in.". Is there a way of throwing a
> SoapException which doesn't provide this additional information to the
> client? Maybe i'm being petty but i can't imagine getting a login
> failure message from a commercial microsoft web service that gives me
> the line number the error was thrown from.
>
> Is this how other developers are handling these exceptions? If it's
> the best practice i have no real problem with it - it just doesn't
> seem quite right.
>
> any comments appreciated.
>
> Ian
>
> --Error Message being returned--
> System.Web.Services.Protocols.SoapException: Not logged in.
> at SSRAUploadWebService.RAService.CheckLoggedIn() in
> c:\inetpub\wwwroot\SSRAUpload\RAService.asmx.vb:line 81
> at SSRAUploadWebService.RAService.UploadBatch(XmlDocument XMLDoc)
> in c:\inetpub\wwwroot\SSRAUpload\RAService.asmx.vb:line 111"
>
> --Code used to throw Exception--
>
> Private Function CheckLoggedIn() As Boolean
> If (sessionLoginSecurity Is Nothing) OrElse
> sessionLoginSecurity.IsLoggedIn = False Then
> Throw RaiseException("checkLoggedIn", "Not logged in.",
> errorCodes.NotLoggedIn, "", FaultCode.Client)
> End If
>
> End Function
>
> Public Function RaiseException(ByVal uri As String, _
> ByVal errorMessage As String, _
> ByVal errorNumber As errorCodes, _
> ByVal errorSource As String, _
> ByVal code As FaultCode) As SoapException
>
> Dim webServiceNamespace As String =
> "http://flexirent.com/webServices/SSRAUpload/RAService....
> Dim faultCodeLocation As XmlQualifiedName
> 'Identify the location of the FaultCode
> Select Case code
> Case FaultCode.Client
> faultCodeLocation = SoapException.ClientFaultCode
>
> Case FaultCode.Server
> faultCodeLocation = SoapException.ServerFaultCode
> End Select
>
>
> Dim xmlDoc As XmlDocument = New XmlDocument
>
> 'Create the Detail node
> Dim rootNode As XmlNode =
> xmlDoc.CreateNode(XmlNodeType.Element, _
>
> SoapException.DetailElementName.Name, _
>
> SoapException.DetailElementName.Namespace)
>
> 'Build specific details for the SoapException
> 'Add first child of detail XML element.
> Dim errorNode As XmlNode =
> xmlDoc.CreateNode(XmlNodeType.Element, "Error", webServiceNamespace)
>
> 'Create and set the value for the ErrorNumber node
> 'Dim errorNumberNode As XmlNode =
> xmlDoc.CreateNode(XmlNodeType.Element, "ErrorNumber",
> webServiceNamespace)
> 'errorNumberNode.InnerText = errorNumber
>
> 'Create and set the value for the ErrorMessage node
> Dim errorMessageNode As XmlNode =
> xmlDoc.CreateNode(XmlNodeType.Element, "ErrorMessage",
> webServiceNamespace)
> errorMessageNode.InnerText = errorMessage
>
> 'Create and set the value for the ErrorSource node
> Dim errorSourceNode As XmlNode =
> xmlDoc.CreateNode(XmlNodeType.Element, "ErrorSource",
> webServiceNamespace)
> errorSourceNode.InnerText = errorSource
>
> 'Append the Error child element nodes to the root detail node.
> 'errorNode.AppendChild(errorNumberNode)
> errorNode.AppendChild(errorMessageNode)
> errorNode.AppendChild(errorSourceNode)
>
> 'Append the Detail node to the root node
> rootNode.AppendChild(errorNode)
>
> 'Construct the exception
> Dim soapEx As SoapException = New SoapException(errorMessage,
> faultCodeLocation, uri, rootNode)
>
> 'Raise the exception back to the caller
> Return soapEx
>
> End Function