SamplesExceptionHelper.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2008
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. // Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  9. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  10. // sn_dg_sdk_exception_start_1
  11. using System;
  12. using System.Web.Services.Protocols;
  13. using System.Xml;
  14. using System.Text;
  15. namespace SamplesCommon
  16. {
  17. /// <summary>
  18. /// Simple exception handling object for use with IBM Cognos.
  19. /// </summary>
  20. public class ExceptionHelper
  21. {
  22. private SoapException _exception = null;
  23. /// <summary>
  24. /// Create an ExceptionHelper object.
  25. /// </summary>
  26. /// <param name="ex">A SoapException thrown by a call to an IBM Cognos service.</param>
  27. public ExceptionHelper( SoapException ex )
  28. {
  29. _exception = ex;
  30. }
  31. /// <summary>
  32. /// Return the exception message.
  33. /// </summary>
  34. public string Message
  35. {
  36. get
  37. {
  38. return _exception.Message;
  39. }
  40. }
  41. /// <summary>
  42. /// Return the exception severity.
  43. /// </summary>
  44. public string Severity
  45. {
  46. get
  47. {
  48. XmlNode severityNode = _exception.Detail.SelectSingleNode( "//severity" );
  49. if (severityNode != null)
  50. {
  51. return severityNode.InnerText;
  52. }
  53. return "";
  54. }
  55. }
  56. /// <summary>
  57. /// Return the exception errorCode.
  58. /// </summary>
  59. public string ErrorCode
  60. {
  61. get
  62. {
  63. XmlNode errorNode = _exception.Detail.SelectSingleNode( "//errorCode" );
  64. if (errorNode != null)
  65. {
  66. return errorNode.InnerText;
  67. }
  68. return "";
  69. }
  70. }
  71. /// <summary>
  72. /// Return the exception messageStrings.
  73. /// </summary>
  74. public string[] Details {
  75. get {
  76. XmlNodeList nodes = _exception.Detail.SelectNodes( "//messageString" );
  77. string[] retval = new string[nodes.Count];
  78. for( int idx = 0; idx < nodes.Count; idx++ ) {
  79. retval[idx] = nodes[idx].InnerText;
  80. }
  81. return retval;
  82. }
  83. }
  84. /// <summary>
  85. /// Convert this exception into a string for printing.
  86. /// </summary>
  87. /// <returns>A string representation of the exception.</returns>
  88. public override string ToString() {
  89. StringBuilder str = new StringBuilder();
  90. str.AppendFormat( "Message: {0}\n", Message );
  91. str.AppendFormat( "Severity: {0}\n", Severity );
  92. str.AppendFormat( "ErrorCode: {0}\n", ErrorCode );
  93. str.AppendFormat( "Details:\n" );
  94. foreach( string s in Details ) {
  95. str.AppendFormat( "\t{0}\n", s );
  96. }
  97. return str.ToString();
  98. }
  99. /// <summary>
  100. /// Convert a SoapException into an exception string.
  101. ///
  102. /// This is the same as creating an ExceptionHelper and calling
  103. /// its ToString() method.
  104. /// </summary>
  105. /// <param name="ex">The SoapException to format.</param>
  106. /// <returns>A string representation of the exception.</returns>
  107. static public string ConvertToString( SoapException ex ) {
  108. ExceptionHelper exception = new ExceptionHelper( ex );
  109. return exception.ToString();
  110. }
  111. }
  112. }
  113. // sn_dg_sdk_exception_end_1