How to handle common/routine exceptions?

I

IanT

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.asmx"
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
 
I

IanT

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

many thanks.

<customErrors mode="RemoteOnly" />

was the answer. I had seen this elsewhere but had somehow not got the
desired result in the past.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top