< Summary - Helper.Tests

Information
Class: Application.Helper.GenericException
Assembly: Application.Helper
File(s): D:\a\NuGetPackages\NuGetPackages\src\Helper\Application.Helper\GenericException.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 50
Uncovered lines: 0
Coverable lines: 50
Total lines: 98
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
FormatException(...)100%1414100%
FormatException(...)100%66100%

File(s)

D:\a\NuGetPackages\NuGetPackages\src\Helper\Application.Helper\GenericException.cs

#LineLine coverage
 1using System;
 2
 3namespace 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>
 111    public GenericException()
 212    { }
 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>
 218    public GenericException( string message ) : base( message )
 419    { }
 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>
 128    public GenericException( string message, Exception inner ) : base( message, inner )
 229    { }
 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 )
 739    {
 740      string retValue = string.Empty;
 41
 742      if( null == exception )
 143      {
 144        retValue = "Undefined exception encountered.";
 145      }
 646      else if( exception is AggregateException ae )
 147      {
 748        foreach( Exception ex in ae.InnerExceptions )
 249        {
 250          if( retValue.Length > 0 )
 151          {
 152            retValue += Environment.NewLine;
 153          }
 254          retValue = $"{retValue}{ex.GetType()}: {ex.Message}";
 255          Exception inner = ex;
 456          while( inner != null )
 257          {
 258            if( inner.StackTrace != null )
 159            {
 160              retValue = $"{retValue}{Environment.NewLine}{inner.StackTrace}";
 161            }
 262            inner = inner.InnerException;
 263          }
 264        }
 165      }
 566      else if( exception is GenericException )
 167      {
 168        retValue = exception.Message;
 169      }
 70      else
 471      {
 472        retValue = exception.ToString();
 473      }
 74
 775      return retValue;
 776    }
 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 )
 283    {
 284      msg = string.IsNullOrWhiteSpace( msg ) ? string.Empty : msg.Trim();
 85
 286      if( exception != null )
 287      {
 88        // Add a line break after the message
 589        if( msg.Length > 0 ) { msg += Environment.NewLine; }
 290        msg += FormatException( exception );
 291      }
 92
 293      return msg;
 294    }
 95
 96    #endregion
 97  }
 98}