Purchase your Section 508 Compliance Support guide now!

Purchase your Section 508 Compliance Support guide now!

Cognos SDK -- customized exception handler

We all make mistakes and when comes to programming I’m sure that I’ve made enough to fill an infinite loop array.  Here is some sample code to help you create a customized Exception Handler class.  Once again the Cognos SDK code is in C# for all of us .NET users.

 

using System;

using System.Web.Services.Protocols;

using System.Xml;

using System.Text;

using cognosdotnet;

 

namespace CognosSDK

{

       /// <summary>

       /// Handles SOAP exceptions.

       /// </summary>

       public class SoapExceptionHandler

       {            

              private SoapException exSoap = null;

 

              public SoapExceptionHandler(SoapException ex)

              {

                     exSoap = ex;

              }

 

              public string Message

              {

                     get

                     {

                           return exSoap.Message;

                     }

              }

              /// <summary> /// Return the exception severity. /// </summary>

              public string Severity

              {

                     get

                     {

                           XmlNode severityNode = exSoap.Detail.SelectSingleNode( "//severity");

                           if (severityNode != null)

                           {

                                  return severityNode.InnerText;

                           }

                           return "";

                     }

              }

              /// <summary> /// Return the exception errorCode. /// </summary>

              public string ErrorCode

              {

                     get

                     {

                           XmlNode errorNode = exSoap.Detail.SelectSingleNode( "//errorCode" );

                           if (errorNode != null)

                           {

                                  return errorNode.InnerText;

                           }

                           return "";

                     }

              }

              /// <summary> /// Return the exception messageStrings. /// </summary>

              public string[] Details

              {

                     get

                     {

                           XmlNodeList nodes = exSoap.Detail.SelectNodes( "//messageString" );

                           string[] retval = new string[nodes.Count];

                           for( int idx = 0; idx < nodes.Count; idx++ )

                           {

                                  retval[idx] = nodes[idx].InnerText;

                           }

                           return retval;

                     }

              }

              /// <summary> /// Convert this Cognos8Exception into a string for printing. </summary>

              /// <returns>A string representation of the Cognos8Exception.</returns>

              public override string ToString()

              {

                     StringBuilder str = new StringBuilder();

                     str.AppendFormat( "Message: {0}\n", Message );

                     str.AppendFormat( "Severity: {0}\n", Severity );

                     str.AppendFormat( "ErrorCode: {0}\n", ErrorCode );

                     str.AppendFormat( "Details:\n" );

                     foreach( string s in Details )

                     {

                           str.AppendFormat( "\t{0}\n", s );

                     }

                    

                     return str.ToString();

       }

       /// <summary> Convert a SoapException into a Cognos8Exception string. This is the same as creating a              Cognos8Exception and calling its ToString() method.

       /// </summary>

       /// <param name="ex">The SoapException to format.</param>

       /// <returns>A string representation of the exception.</returns>

      

       static public string ConvertToString(SoapException ex)

       {

              SoapExceptionHandler exception = new SoapExceptionHandler(ex);

              return exception.ToString();

       }

       }

 

 

       /// <summary>

       /// Handles BiBus Exceptions

       /// </summary>

       public class BiBusHeaderException

       {

              private CAMException exBiBus = null;

              /// <summary>

              /// Create a BiBusHeaderException object.

              /// </summary>

              /// <param name="crn">The Service object in use during the last exception.</param>

              public BiBusHeaderException( contentManagerService1 cmService )

              {

                     // Pull the CAM exception out of the biBusHeader.

                     exBiBus = cmService.biBusHeaderValue.CAM.exception;

              }

              /// <summary>

              /// Get the Severity string from this BiBusHeaderException.

              /// </summary>

              public string Severity

              {

                     get

                     {

                           return exBiBus.severity.ToString();

                     }

              }

              /// <summary>

              /// Get the errorCode string from this BiBusHeaderException.

              /// </summary>

              public string ErrorCode

              {

                     get

                     {

                           return exBiBus.errorCodeString;

                     }

              }

              /// <summary>

              /// Get the details (messageString) from this BiBusHeaderException.

 

              /// </summary>

              public string[] Details

              {

                     get

                     {

                           string[] retval = new string[exBiBus.messages.Length];

                           for( int idx = 0; idx < exBiBus.messages.Length; idx++ )

                           {

                                  retval[idx] = exBiBus.messages[idx].messageString;

                           }

                           return retval;

                     }

              }

              /// <summary>

              /// Get the promptInfo (and useful captions/displayObjects inside) to

              /// facilitate prompting the user, if this is a recoverable exception.

              /// </summary>

              public promptInfo PromptInfo

              {

                     get

                     {

                           return exBiBus.promptInfo;

                     }

              }

              /// <summary>

              /// Convert this BiBusHeaderException into a string for printing.

              /// </summary>

              /// <returns>A string representation of the BiBusHeaderException.</returns>

              public override string ToString()

              {

                     StringBuilder str = new StringBuilder();

                     str.AppendFormat( "Severity: {0}\n", Severity );

                     str.AppendFormat( "ErrorCode: {0}\n", ErrorCode );

                     str.AppendFormat( "Details:\n" );

                     foreach( string s in Details )

                     {

                           str.AppendFormat( "\t{0}\n", s );

                     }

                     return str.ToString();

              }

              /// <summary>

              /// Convert a BiBus Header exception into a BiBusHeaderException string.

              /// This is the same as creating a BiBusHeaderException and calling

              /// its ToString() method.

              /// </summary>

              /// <param name="ex">The Service object that threw the exception.</param>

              /// <returns>A string representation of the biBusHeader exception.</returns>

              static public string ConvertToString( contentManagerService1 cmService )

              {

                     BiBusHeaderException exception = new BiBusHeaderException( cmService );

                     return exception.ToString();

              }

       }

}