How to handle common/routine exceptions?

Discussion in 'ASP .Net Web Services' started by IanT, Jun 9, 2004.

  1. IanT

    IanT Guest

    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
     
    IanT, Jun 9, 2004
    #1
    1. Advertising

  2. IanT

    Jan Tielens Guest

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

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

    --
    Greetz
    Jan
    ________________
    Read my weblog: http://weblogs.asp.net/jan


    "IanT" <> schreef in bericht
    news:...
    > 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
     
    Jan Tielens, Jun 9, 2004
    #2
    1. Advertising

  3. IanT

    IanT Guest

    >
    > 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.
     
    IanT, Jun 17, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gamename
    Replies:
    5
    Views:
    459
  2. Denny
    Replies:
    1
    Views:
    822
  3. lorlarz
    Replies:
    11
    Views:
    184
    lorlarz
    Sep 9, 2008
  4. John Reye
    Replies:
    28
    Views:
    1,420
    Tim Rentsch
    May 8, 2012
  5. Jason Mellone
    Replies:
    3
    Views:
    140
    Jurko Gospodnetić
    May 7, 2014
Loading...

Share This Page