ExceptionExc.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Quadarax.Foundation.Core.Exceptions
  4. {
  5. public static class ExceptionExc
  6. {
  7. public static int GetCode(this Exception e)
  8. {
  9. if (!e.Data.Contains("Code"))
  10. return -1;
  11. return int.Parse(e.Data["Code"]?.ToString() ?? "-1");
  12. }
  13. public static void SetCode(this Exception e, int code)
  14. {
  15. e.Data.Add("Code", code);
  16. }
  17. public static string Dump(this Exception e)
  18. {
  19. var level = 0;
  20. var sb = new System.Text.StringBuilder();
  21. sb.Append("[#").Append(level).Append("]:").AppendLine(e.Message);
  22. var inner = e.InnerException;
  23. while (inner != null)
  24. {
  25. sb.Append("[#").Append(level).Append("]:").AppendLine(inner.Message);
  26. inner = inner.InnerException;
  27. }
  28. if (e.StackTrace != null)
  29. {
  30. sb.AppendLine("[#0] Stack list:");
  31. sb.AppendLine(e.StackTrace);
  32. }
  33. return sb.ToString();
  34. }
  35. /// <summary>
  36. /// Converts an Exception and its inner exceptions to an AggregateException.
  37. /// Flattens the exception hierarchy into a single AggregateException containing all exceptions.
  38. /// </summary>
  39. /// <param name="exception">The exception to convert</param>
  40. /// <returns>An AggregateException containing the original exception and all inner exceptions</returns>
  41. public static AggregateException ToAggregateException(this Exception exception)
  42. {
  43. if (exception == null)
  44. throw new ArgumentNullException(nameof(exception));
  45. // If it's already an AggregateException, flatten it to avoid nested AggregateExceptions
  46. if (exception is AggregateException aggregateException)
  47. {
  48. return aggregateException.Flatten();
  49. }
  50. var exceptions = new List<Exception>();
  51. FlattenExceptions(exception, exceptions);
  52. return new AggregateException(exceptions);
  53. }
  54. /// <summary>
  55. /// Recursively flattens exception hierarchy into a list
  56. /// </summary>
  57. /// <param name="exception">Current exception to process</param>
  58. /// <param name="exceptions">List to accumulate flattened exceptions</param>
  59. private static void FlattenExceptions(Exception exception, List<Exception> exceptions)
  60. {
  61. if (exception == null)
  62. return;
  63. // Handle AggregateException specially to avoid deep nesting
  64. if (exception is AggregateException aggEx)
  65. {
  66. foreach (var innerEx in aggEx.InnerExceptions)
  67. {
  68. FlattenExceptions(innerEx, exceptions);
  69. }
  70. }
  71. else
  72. {
  73. // Add the current exception
  74. exceptions.Add(exception);
  75. // Recursively process inner exception
  76. if (exception.InnerException != null)
  77. {
  78. FlattenExceptions(exception.InnerException, exceptions);
  79. }
  80. }
  81. }
  82. }
  83. }