Skip to content

Provides helper functions for exceptions to get all exceptions in the InnerException hirachy and to build full messages out of it

License

Notifications You must be signed in to change notification settings

DCCS-IT-Business-Solutions/DCCS.ExceptionHelpers.NetStandard

Repository files navigation

DCCS.ExceptionHelpers.NetStandard Build status NuGet Badge

Provides helper functions for exceptions to get all exceptions in the InnerException hierarchy and to build full messages out of it.

Note: The AggregateException which have multiple InnerExceptions will be handled too

Installation

Install DCCS.ExceptionHelpers.NetStandard with NuGet:

Install-Package DCCS.ExceptionHelpers.NetStandard

Or via the .NET Core command line interface:

dotnet add package DCCS.ExceptionHelpers.NetStandard

Either commands, from Package Manager Console or .NET Core CLI, will download and install DCCS.ExceptionHelpers.NetStandard.

API

Available extension methods in this package:

    // Return the message of the exception and all inner exceptions.
    public static string BuildCompleteMessage(this Exception exception, bool includeCallstack, string separator = null)

    // Returns the exception and all inner exceptions
    public static IEnumerable<Exception> GetAllExceptionsInHierarchy(this Exception exception)

Examples

Sample usage of the GetRecursiveMessage message:

// Add the namespace to your file to get the extension methods
using DCCS.ExceptionHelpers.NetStandard;

class Program
{
    public void Main()
    {
        try
        {
            ReadConfig(); // Will throw an exception
        }
        catch (Exception e)
        {
            // Trace all exceptions in the stack including the call stack
            Trace.WriteLine(e.BuildCompleteMessage(true));
                
            // Write all exception messages to the console (without the call stack)
            Console.WriteLine(e.BuildCompleteMessage(false)); 
        }
    }

    void ReadConfig()
    {
        try
        {
            // This will raise an FileNotFound exception for demonstation
            using (var configFile = File.OpenRead(Path.Combine(Path.GetTempPath(), @"NOTEXISTINGCONFIGFILE.XXX")))
            {

            }
        }
        catch (Exception e)
        {
            throw new Exception("Read configuration failed", e);
        }
    }
}

Contributing

All contributors are welcome to improve this project. The best way would be to start a bug or feature request and discuss the way you want find a solution with us. After this process, please create a fork of the repository and open a pull request with your changes. Of course, if your changes are small, feel free to immediately start your pull request without previous discussion.

About

Provides helper functions for exceptions to get all exceptions in the InnerException hirachy and to build full messages out of it

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages