From 7037e2dc5e3a8cd4e58180233f743f0d62c44407 Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Sun, 28 Nov 2021 22:01:57 +0000 Subject: [PATCH] feat(*): Add ExceptionExtension class --- Creek.HelpfulExtensions/ExceptionExtension.cs | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Creek.HelpfulExtensions/ExceptionExtension.cs diff --git a/Creek.HelpfulExtensions/ExceptionExtension.cs b/Creek.HelpfulExtensions/ExceptionExtension.cs new file mode 100644 index 0000000..981a768 --- /dev/null +++ b/Creek.HelpfulExtensions/ExceptionExtension.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Creek.HelpfulExtensions +{ + public static class ExceptionExtension + { + /// + /// Returns a string of the message of the first/root/base exception in an exception stack. + /// + /// The Exception stack to find the first/root/base exception within. + /// Returns the first/root/base exception message string. + public static string BaseExceptionMessage(this Exception ex) + { + Exception baseEx = ex.GetBaseException(); + return baseEx.Message; + } + + /// + /// Returns a single string of the message and stack trace of the first/root/base exception in an exception stack. + /// + /// The Exception stack to find the first/root/base exception within. + /// Returns the first/root/base exception message and stack trace as a single string, separated by " :: ". + public static string BaseExceptionMessageAndStackTrace(this Exception ex) + { + Exception baseEx = ex.GetBaseException(); + return GenerateMessageAndStackTraceString(baseEx); + } + + /// + /// Returns a list of the exception messages of each nested exception in an exception stack. + /// + /// The Exception stack. + /// Returns a list of the exception messages of each nested exception in an exception stack. + public static List AllInnerExceptionMessages(this Exception ex) + { + return GetAllInnerExceptionMessagesAndStackTraces(ex, includeStackTraces: false); + } + + /// + /// Returns a list of single strings of the message and stack trace of each nested exception in an exception stack. + /// + /// The Exception stack. + /// Returns a list with a string for each nested exception in an exception stack containing the exception message and stack trace as a single string, separated by " :: ". + public static List AllInnerExceptionMessagesAndStackTraces(this Exception ex) + { + return GetAllInnerExceptionMessagesAndStackTraces(ex, includeStackTraces: true); + } + + private static List GetAllInnerExceptionMessagesAndStackTraces(Exception ex, bool includeStackTraces) + { + List exceptionMessagesAndStackTraces = new List(); + + Exception inner = ex.InnerException; + + exceptionMessagesAndStackTraces.Add( + includeStackTraces ? GenerateMessageAndStackTraceString(ex) + : ex.Message); + + while (inner != null) + { + exceptionMessagesAndStackTraces.Add( + includeStackTraces ? GenerateMessageAndStackTraceString(inner) + : inner.Message); + inner = inner.InnerException; + } + + // We've now reached the end of the exceptions, so can return the error messages + return exceptionMessagesAndStackTraces; + } + + private static string GenerateMessageAndStackTraceString(Exception ex) + { + return $"{ex.Message} :: {ex.StackTrace}"; + } + } +}