|   |  | 1 |  | using System; | 
|   |  | 2 |  |  | 
|   |  | 3 |  | namespace Application.Helper | 
|   |  | 4 |  | { | 
|   |  | 5 |  |   /// <summary>Represents errors that occur during application execution.</summary> | 
|   |  | 6 |  |   public class GenericException : Exception | 
|   |  | 7 |  |   { | 
|   |  | 8 |  |     #region Constructors | 
|   |  | 9 |  |  | 
|   |  | 10 |  |     /// <summary>Initializes a new instance of the GenericException class.</summary> | 
|   | 1 | 11 |  |     public GenericException() | 
|   | 2 | 12 |  |     { } | 
|   |  | 13 |  |  | 
|   |  | 14 |  |     /// <summary> | 
|   |  | 15 |  |     /// Initializes a new instance of the GenericException class with a specified error message. | 
|   |  | 16 |  |     /// </summary> | 
|   |  | 17 |  |     /// <param name="message">The message that describes the error.</param> | 
|   | 2 | 18 |  |     public GenericException( string message ) : base( message ) | 
|   | 4 | 19 |  |     { } | 
|   |  | 20 |  |  | 
|   |  | 21 |  |     /// <summary> | 
|   |  | 22 |  |     /// Initializes a new instance of the class with a specified error message | 
|   |  | 23 |  |     /// and a reference to the inner exception that is the cause of this exception. | 
|   |  | 24 |  |     /// </summary> | 
|   |  | 25 |  |     /// <param name="message">The error message that explains the reason for the exception.</param> | 
|   |  | 26 |  |     /// <param name="inner">The exception that is the cause of the current exception, | 
|   |  | 27 |  |     /// or a null reference if no inner exception is specified.</param> | 
|   | 1 | 28 |  |     public GenericException( string message, Exception inner ) : base( message, inner ) | 
|   | 2 | 29 |  |     { } | 
|   |  | 30 |  |  | 
|   |  | 31 |  |     #endregion | 
|   |  | 32 |  |  | 
|   |  | 33 |  |     #region Public Methods | 
|   |  | 34 |  |  | 
|   |  | 35 |  |     /// <summary>Formats an exception as a string.</summary> | 
|   |  | 36 |  |     /// <param name="exception">Exception to format.</param> | 
|   |  | 37 |  |     /// <returns>String containing the exception details.</returns> | 
|   |  | 38 |  |     public static string FormatException( Exception exception ) | 
|   | 7 | 39 |  |     { | 
|   | 7 | 40 |  |       string retValue = string.Empty; | 
|   |  | 41 |  |  | 
|   | 7 | 42 |  |       if( null == exception ) | 
|   | 1 | 43 |  |       { | 
|   | 1 | 44 |  |         retValue = "Undefined exception encountered."; | 
|   | 1 | 45 |  |       } | 
|   | 6 | 46 |  |       else if( exception is AggregateException ae ) | 
|   | 1 | 47 |  |       { | 
|   | 7 | 48 |  |         foreach( Exception ex in ae.InnerExceptions ) | 
|   | 2 | 49 |  |         { | 
|   | 2 | 50 |  |           if( retValue.Length > 0 ) | 
|   | 1 | 51 |  |           { | 
|   | 1 | 52 |  |             retValue += Environment.NewLine; | 
|   | 1 | 53 |  |           } | 
|   | 2 | 54 |  |           retValue = $"{retValue}{ex.GetType()}: {ex.Message}"; | 
|   | 2 | 55 |  |           Exception inner = ex; | 
|   | 4 | 56 |  |           while( inner != null ) | 
|   | 2 | 57 |  |           { | 
|   | 2 | 58 |  |             if( inner.StackTrace != null ) | 
|   | 1 | 59 |  |             { | 
|   | 1 | 60 |  |               retValue = $"{retValue}{Environment.NewLine}{inner.StackTrace}"; | 
|   | 1 | 61 |  |             } | 
|   | 2 | 62 |  |             inner = inner.InnerException; | 
|   | 2 | 63 |  |           } | 
|   | 2 | 64 |  |         } | 
|   | 1 | 65 |  |       } | 
|   | 5 | 66 |  |       else if( exception is GenericException ) | 
|   | 1 | 67 |  |       { | 
|   | 1 | 68 |  |         retValue = exception.Message; | 
|   | 1 | 69 |  |       } | 
|   |  | 70 |  |       else | 
|   | 4 | 71 |  |       { | 
|   | 4 | 72 |  |         retValue = exception.ToString(); | 
|   | 4 | 73 |  |       } | 
|   |  | 74 |  |  | 
|   | 7 | 75 |  |       return retValue; | 
|   | 7 | 76 |  |     } | 
|   |  | 77 |  |  | 
|   |  | 78 |  |     /// <summary>Formats an exception as a string.</summary> | 
|   |  | 79 |  |     /// <param name="msg">Additional message to include.</param> | 
|   |  | 80 |  |     /// <param name="exception">Exception to format.</param> | 
|   |  | 81 |  |     /// <returns>String containing the exception details.</returns> | 
|   |  | 82 |  |     public static string FormatException( string msg, Exception exception ) | 
|   | 2 | 83 |  |     { | 
|   | 2 | 84 |  |       msg = string.IsNullOrWhiteSpace( msg ) ? string.Empty : msg.Trim(); | 
|   |  | 85 |  |  | 
|   | 2 | 86 |  |       if( exception != null ) | 
|   | 2 | 87 |  |       { | 
|   |  | 88 |  |         // Add a line break after the message | 
|   | 5 | 89 |  |         if( msg.Length > 0 ) { msg += Environment.NewLine; } | 
|   | 2 | 90 |  |         msg += FormatException( exception ); | 
|   | 2 | 91 |  |       } | 
|   |  | 92 |  |  | 
|   | 2 | 93 |  |       return msg; | 
|   | 2 | 94 |  |     } | 
|   |  | 95 |  |  | 
|   |  | 96 |  |     #endregion | 
|   |  | 97 |  |   } | 
|   |  | 98 |  | } |