Last active
February 7, 2024 11:26
-
-
Save benbrandt22/8676438 to your computer and use it in GitHub Desktop.
C# Extension method that generates a list of exception messages from the top level down through all inner exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace System { | |
public static partial class ExceptionExtensions { | |
/// <summary> | |
/// Returns a list of all the exception messages from the top-level | |
/// exception down through all the inner exceptions. Useful for making | |
/// logs and error pages easier to read when dealing with exceptions. | |
/// Usage: Exception.Messages() | |
/// </summary> | |
public static IEnumerable<string> Messages( this Exception ex ) { | |
// return an empty sequence if the provided exception is null | |
if (ex == null) { yield break; } | |
// first return THIS exception's message at the beginning of the list | |
yield return ex.Message; | |
// then get all the lower-level exception messages recursively (if any) | |
IEnumerable<Exception> innerExceptions = Enumerable.Empty<Exception>(); | |
if (ex is AggregateException && (ex as AggregateException).InnerExceptions.Any()) | |
{ | |
innerExceptions = (ex as AggregateException).InnerExceptions; | |
} | |
else if (ex.InnerException != null) | |
{ | |
innerExceptions = new Exception[] { ex.InnerException }; | |
} | |
foreach (var innerEx in innerExceptions) | |
{ | |
foreach (string msg in innerEx.Messages()) | |
{ | |
yield return msg; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's about Exception.GetBaseException() ? https://stackoverflow.com/questions/2085196/proper-way-to-find-the-innermost-exception