/HDLGUtility/Exceptions.cs
C# | 175 lines | 120 code | 21 blank | 34 comment | 40 complexity | e158d0b4c0d1694866a50537a59b4fae MD5 | raw file
Possible License(s): GPL-3.0
- /*
- Copyright 2011 Martin Labelle
-
- This file is part of HTML Directory List Generator.
-
- HTML Directory List Generator is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- HTML Directory List Generator is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with HTML Directory List Generator. If not, see <http://www.gnu.org/licenses/>.
-
- */
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.SqlClient;
- using System.Collections;
- using System.Data.Odbc;
- using System.IO;
- using System.Web;
- using System.Net;
-
- namespace HDLGUtility
- {
- public static class Exceptions
- {
- /// <summary>
- /// Print an exception to a TextWriter
- /// </summary>
- /// <param name="writer">TextWriter to write to</param>
- /// <param name="exc">Exception</param>
- public static void PrintException(TextWriter writer, Exception exc)
- {
- PrintException(writer, exc, null);
- }
-
- /// <summary>
- /// Print an exception to a TextWriter
- /// </summary>
- /// <param name="writer">TextWriter to write to</param>
- /// <param name="exc">Exception</param>
- /// <param name="smallMessage">A message to print</param>
- public static void PrintException(TextWriter writer, Exception exc, string smallMessage)
- {
- Exception excTemp = exc;
-
-
- while (excTemp != null)
- {
- //Write main exception information
- //logger.LogException(level, "Exception (" + excTemp.GetType().ToString() + ") occur in " + exc.TargetSite + "." + (smallMessage != null ? " Message: " + smallMessage : ""), excTemp);
- writer.WriteLine(string.Format("Exception (" + excTemp.GetType().ToString() + ") occur in " + exc.TargetSite + "." + (smallMessage != null ? " Message: " + smallMessage : ""), excTemp));
-
- try
- {
- //Detect the type of the extension and write detailed information of this extension
- if (excTemp.GetType() == typeof(SqlException))
- {
- SqlException excSql = (SqlException)excTemp;
-
- SqlErrorCollection errors = excSql.Errors;
- StringBuilder strErrors = new StringBuilder();
-
- if (errors.Count > 0)
- {
- foreach (SqlError error in errors)
- {
- 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));
- }
- }
-
- writer.WriteLine(string.Format("Line number: {0}, Procedure: {1}, Server: {2}", excSql.LineNumber, excSql.Procedure, excSql.Server));
- writer.WriteLine(string.Format("Errors details: {0}", strErrors.ToString()));
- }
- else if (excTemp.GetType() == typeof(OdbcException))
- {
- OdbcException odbcExc = (OdbcException)excTemp;
-
- OdbcErrorCollection errors = odbcExc.Errors;
-
- StringBuilder strErrors = new StringBuilder();
-
- if (errors.Count > 0)
- {
- foreach (OdbcError error in errors)
- {
- strErrors.AppendLine(string.Format("Error {0}: NativeError: {1}, SQLState: {2}, Source: {3}", error.Message.Trim(), error.NativeError, error.SQLState.Trim(), error.Source.Trim()));
- }
- }
-
- writer.WriteLine(string.Format("HRESULT: {0}", odbcExc.ErrorCode));
-
- writer.WriteLine(string.Format("Errors details: {0}", strErrors.ToString()));
-
- }
- else if (excTemp.GetType() == typeof(HttpException))
- {
- HttpException heExc = (HttpException)excTemp;
-
- writer.WriteLine(string.Format("HTTP error code: {0}", heExc.GetHttpCode()));
- writer.WriteLine(string.Format("HRESULT: {0}", heExc.ErrorCode));
-
- }
- else if (excTemp.GetType() == typeof(FileNotFoundException))
- {
- FileNotFoundException fnfexc = (FileNotFoundException)excTemp;
- writer.WriteLine(string.Format("Name of the file not found: {0}", fnfexc.FileName));
- }
- else if (excTemp.GetType() == typeof(ArgumentException))
- {
- ArgumentException aExc = (ArgumentException)excTemp;
- writer.WriteLine(string.Format("ParamName: {0}", aExc.ParamName));
- }
- else if (excTemp.GetType() == typeof(ArgumentNullException))
- {
- ArgumentNullException anExc = (ArgumentNullException)excTemp;
- writer.WriteLine(string.Format("ParamName: {0}", anExc.ParamName));
- }
- else if (excTemp.GetType() == typeof(ArgumentOutOfRangeException))
- {
- ArgumentOutOfRangeException aorExc = (ArgumentOutOfRangeException)excTemp;
- writer.WriteLine(string.Format("ParamName: {0}", aorExc.ParamName));
- if (aorExc.ActualValue != null)
- {
- writer.WriteLine(string.Format("ActualValue: {0}", aorExc.ActualValue.ToString().Trim()));
- }
- }
- else if (excTemp.GetType() == typeof(WebException))
- {
- WebException webExc = (WebException)excTemp;
- writer.WriteLine(string.Format("Status: {0}", webExc.Status.ToString()));
- }
- else if (excTemp.GetType() == typeof(FileLoadException))
- {
- FileLoadException fle = (FileLoadException)excTemp;
- if (fle.FileName != null)
- {
- writer.WriteLine(string.Format("File name: {0}", fle.FileName));
- }
- if (fle.FusionLog != null)
- {
- writer.WriteLine(string.Format("fle.FusionLog: {0}", fle.FusionLog));
- }
- }
-
-
- //Look if this exception got extra data and log it
- if ((excTemp.Data != null) && (excTemp.Data.Count > 0))
- {
- writer.WriteLine(string.Format("Extra details:"));
- foreach (DictionaryEntry de in excTemp.Data)
- writer.WriteLine(string.Format("Key: '{0}' exc: {1}",
- de.Key, de.Value));
- }
-
- }
- catch (Exception errorExc)
- {
- writer.WriteLine(string.Format("An error occured while logging exception's data", errorExc));
- }
-
- excTemp = excTemp.InnerException;
- }
- }
- }
- }