PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/HDLGUtility/Exceptions.cs

https://bitbucket.org/bestter/hdlg
C# | 175 lines | 120 code | 21 blank | 34 comment | 40 complexity | e158d0b4c0d1694866a50537a59b4fae MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. Copyright 2011 Martin Labelle
  3. This file is part of HTML Directory List Generator.
  4. HTML Directory List Generator is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. HTML Directory List Generator is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with HTML Directory List Generator. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Data.SqlClient;
  20. using System.Collections;
  21. using System.Data.Odbc;
  22. using System.IO;
  23. using System.Web;
  24. using System.Net;
  25. namespace HDLGUtility
  26. {
  27. public static class Exceptions
  28. {
  29. /// <summary>
  30. /// Print an exception to a TextWriter
  31. /// </summary>
  32. /// <param name="writer">TextWriter to write to</param>
  33. /// <param name="exc">Exception</param>
  34. public static void PrintException(TextWriter writer, Exception exc)
  35. {
  36. PrintException(writer, exc, null);
  37. }
  38. /// <summary>
  39. /// Print an exception to a TextWriter
  40. /// </summary>
  41. /// <param name="writer">TextWriter to write to</param>
  42. /// <param name="exc">Exception</param>
  43. /// <param name="smallMessage">A message to print</param>
  44. public static void PrintException(TextWriter writer, Exception exc, string smallMessage)
  45. {
  46. Exception excTemp = exc;
  47. while (excTemp != null)
  48. {
  49. //Write main exception information
  50. //logger.LogException(level, "Exception (" + excTemp.GetType().ToString() + ") occur in " + exc.TargetSite + "." + (smallMessage != null ? " Message: " + smallMessage : ""), excTemp);
  51. writer.WriteLine(string.Format("Exception (" + excTemp.GetType().ToString() + ") occur in " + exc.TargetSite + "." + (smallMessage != null ? " Message: " + smallMessage : ""), excTemp));
  52. try
  53. {
  54. //Detect the type of the extension and write detailed information of this extension
  55. if (excTemp.GetType() == typeof(SqlException))
  56. {
  57. SqlException excSql = (SqlException)excTemp;
  58. SqlErrorCollection errors = excSql.Errors;
  59. StringBuilder strErrors = new StringBuilder();
  60. if (errors.Count > 0)
  61. {
  62. foreach (SqlError error in errors)
  63. {
  64. strErrors.AppendLine(string.Format("Error {0}: number: {1}, Procedure: {2}, Source: {3}, LineNumber: {4}", error.Message.Trim(), error.Number, error.Procedure.Trim(), error.Source.Trim(), error.LineNumber));
  65. }
  66. }
  67. writer.WriteLine(string.Format("Line number: {0}, Procedure: {1}, Server: {2}", excSql.LineNumber, excSql.Procedure, excSql.Server));
  68. writer.WriteLine(string.Format("Errors details: {0}", strErrors.ToString()));
  69. }
  70. else if (excTemp.GetType() == typeof(OdbcException))
  71. {
  72. OdbcException odbcExc = (OdbcException)excTemp;
  73. OdbcErrorCollection errors = odbcExc.Errors;
  74. StringBuilder strErrors = new StringBuilder();
  75. if (errors.Count > 0)
  76. {
  77. foreach (OdbcError error in errors)
  78. {
  79. strErrors.AppendLine(string.Format("Error {0}: NativeError: {1}, SQLState: {2}, Source: {3}", error.Message.Trim(), error.NativeError, error.SQLState.Trim(), error.Source.Trim()));
  80. }
  81. }
  82. writer.WriteLine(string.Format("HRESULT: {0}", odbcExc.ErrorCode));
  83. writer.WriteLine(string.Format("Errors details: {0}", strErrors.ToString()));
  84. }
  85. else if (excTemp.GetType() == typeof(HttpException))
  86. {
  87. HttpException heExc = (HttpException)excTemp;
  88. writer.WriteLine(string.Format("HTTP error code: {0}", heExc.GetHttpCode()));
  89. writer.WriteLine(string.Format("HRESULT: {0}", heExc.ErrorCode));
  90. }
  91. else if (excTemp.GetType() == typeof(FileNotFoundException))
  92. {
  93. FileNotFoundException fnfexc = (FileNotFoundException)excTemp;
  94. writer.WriteLine(string.Format("Name of the file not found: {0}", fnfexc.FileName));
  95. }
  96. else if (excTemp.GetType() == typeof(ArgumentException))
  97. {
  98. ArgumentException aExc = (ArgumentException)excTemp;
  99. writer.WriteLine(string.Format("ParamName: {0}", aExc.ParamName));
  100. }
  101. else if (excTemp.GetType() == typeof(ArgumentNullException))
  102. {
  103. ArgumentNullException anExc = (ArgumentNullException)excTemp;
  104. writer.WriteLine(string.Format("ParamName: {0}", anExc.ParamName));
  105. }
  106. else if (excTemp.GetType() == typeof(ArgumentOutOfRangeException))
  107. {
  108. ArgumentOutOfRangeException aorExc = (ArgumentOutOfRangeException)excTemp;
  109. writer.WriteLine(string.Format("ParamName: {0}", aorExc.ParamName));
  110. if (aorExc.ActualValue != null)
  111. {
  112. writer.WriteLine(string.Format("ActualValue: {0}", aorExc.ActualValue.ToString().Trim()));
  113. }
  114. }
  115. else if (excTemp.GetType() == typeof(WebException))
  116. {
  117. WebException webExc = (WebException)excTemp;
  118. writer.WriteLine(string.Format("Status: {0}", webExc.Status.ToString()));
  119. }
  120. else if (excTemp.GetType() == typeof(FileLoadException))
  121. {
  122. FileLoadException fle = (FileLoadException)excTemp;
  123. if (fle.FileName != null)
  124. {
  125. writer.WriteLine(string.Format("File name: {0}", fle.FileName));
  126. }
  127. if (fle.FusionLog != null)
  128. {
  129. writer.WriteLine(string.Format("fle.FusionLog: {0}", fle.FusionLog));
  130. }
  131. }
  132. //Look if this exception got extra data and log it
  133. if ((excTemp.Data != null) && (excTemp.Data.Count > 0))
  134. {
  135. writer.WriteLine(string.Format("Extra details:"));
  136. foreach (DictionaryEntry de in excTemp.Data)
  137. writer.WriteLine(string.Format("Key: '{0}' exc: {1}",
  138. de.Key, de.Value));
  139. }
  140. }
  141. catch (Exception errorExc)
  142. {
  143. writer.WriteLine(string.Format("An error occured while logging exception's data", errorExc));
  144. }
  145. excTemp = excTemp.InnerException;
  146. }
  147. }
  148. }
  149. }