| | | 1 | | using System; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | namespace DirectSight.Common; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Exception extensions. |
| | | 9 | | /// </summary> |
| | | 10 | | internal static class ExceptionExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Gets a full error message especially for <see cref="AggregateException"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="exception">The exception.</param> |
| | | 16 | | /// <returns>The error message.</returns> |
| | | 17 | | public static string GetExceptionMessageForDisplay(this Exception exception) |
| | 2 | 18 | | { |
| | 2 | 19 | | if (exception == null) |
| | 0 | 20 | | { |
| | 0 | 21 | | throw new ArgumentNullException(nameof(exception)); |
| | | 22 | | } |
| | | 23 | | |
| | 2 | 24 | | string message = exception.Message; |
| | | 25 | | |
| | 2 | 26 | | if (exception is AggregateException aggregate) |
| | 0 | 27 | | { |
| | 0 | 28 | | var flat = aggregate.Flatten(); |
| | | 29 | | |
| | 0 | 30 | | if (flat.InnerExceptions.Count == 1) |
| | 0 | 31 | | { |
| | | 32 | | // recalling as we potentially can have a targetinvocationexception |
| | 0 | 33 | | message = GetExceptionMessageForDisplay(flat.InnerException); |
| | 0 | 34 | | } |
| | 0 | 35 | | else if (flat.InnerExceptions.Count > 0) |
| | 0 | 36 | | { |
| | 0 | 37 | | var bldr = new StringBuilder(); |
| | | 38 | | |
| | 0 | 39 | | bldr.AppendLine("Multiple errors"); |
| | | 40 | | |
| | 0 | 41 | | foreach (var innerEx in flat.InnerExceptions) |
| | 0 | 42 | | { |
| | 0 | 43 | | bldr.AppendLine(GetExceptionMessageForDisplay(innerEx)); |
| | 0 | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | message = bldr.ToString(); |
| | 0 | 47 | | } |
| | 0 | 48 | | } |
| | | 49 | | else |
| | 2 | 50 | | { |
| | 2 | 51 | | if (exception is TargetInvocationException targetInvocationEx && targetInvocationEx.InnerException != null) |
| | 0 | 52 | | { |
| | 0 | 53 | | message = GetExceptionMessageForDisplay(targetInvocationEx.InnerException); |
| | 0 | 54 | | } |
| | 2 | 55 | | } |
| | | 56 | | |
| | 2 | 57 | | return message; |
| | 2 | 58 | | } |
| | | 59 | | } |