I wrote this code quite a while ago, but since it’s quite reusable (and very simple) here it is:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Services.Protocols;
using System.Xml;
namespace SupportLibrary.ExceptionHandling
{
/// <summary>
/// Wrapper Class for Soap Exception from JBoss.
/// Parses the XML in the Detail XML Node, exposing StatusCode and StatusDescription as properties.
/// </summary>
/// <example>
/// int errorCode = -1;
/// try
/// {
/// WebService.DoSomething();
/// }
/// catch(SoapException soapex)
/// {
/// SoapExceptionWrapper wrapper = new SoapExceptionWrapper(soapex);
/// int errorCode = wrapper.StatusCode;
/// string errorDescription = wrapper.StatusDescription;
/// }
/// </example>
public class SoapExceptionWrapper
{
private SoapException _ex;
/// <summary>
/// The XmlNode containing the Detail information about the error.
/// </summary>
public XmlNode DetailXml
{
get
{
return _ex.Detail.FirstChild;
}
}
/// <summary>
/// Status code from SoapException Detail XML.
/// </summary>
public int StatusCode
{
get
{
string codeStr = "-1";
if(DetailXml != null && DetailXml.ChildNodes.Count > 0)
codeStr = DetailXml.SelectSingleNode("StatusCode").InnerText;
int codeInt = -1;
int.TryParse(codeStr, out codeInt);
return codeInt;
}
}
/// <summary>
/// Status description from SoapException Detail XML.
/// </summary>
public string StatusDescription
{
get
{
if (DetailXml != null && DetailXml.ChildNodes.Count > 0 && DetailXml.SelectSingleNode("StatusDescription") != null && DetailXml.SelectSingleNode("StatusDescription").InnerText != "")
{
return DetailXml.SelectSingleNode("StatusDescription").InnerText;
}
return _ex.Message;
}
}
/// <summary>
/// Constructor using default values.
/// </summary>
/// <param name="soapex"></param>
public SoapExceptionWrapper(SoapException soapex)
{
_ex = soapex;
}
}
}
No comments:
Post a Comment