summaryrefslogtreecommitdiff
path: root/src/SMAPI.Internal/ExceptionExtensions.cs
blob: 5f1ee1faa45582abd76c481589d27995a0158aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Reflection;

namespace StardewModdingAPI.Internal
{
    /// <summary>Provides extension methods for handling exceptions.</summary>
    internal static class ExceptionExtensions
    {
        /*********
        ** Public methods
        *********/
        /// <summary>Get a string representation of an exception suitable for writing to the error log.</summary>
        /// <param name="exception">The error to summarize.</param>
        public static string GetLogSummary(this Exception exception)
        {
            switch (exception)
            {
                case TypeLoadException ex:
                    return $"Failed loading type '{ex.TypeName}': {exception}";

                case ReflectionTypeLoadException ex:
                    string summary = ex.ToString();
                    foreach (Exception childEx in ex.LoaderExceptions ?? new Exception[0])
                        summary += $"\n\n{childEx?.GetLogSummary()}";
                    return summary;

                default:
                    return exception.ToString();
            }
        }

        /// <summary>Get the lowest exception in an exception stack.</summary>
        /// <param name="exception">The exception from which to search.</param>
        public static Exception GetInnermostException(this Exception exception)
        {
            while (exception.InnerException != null)
                exception = exception.InnerException;
            return exception;
        }
    }
}