summaryrefslogtreecommitdiff
path: root/src/SMAPI.Internal/ExceptionHelper.cs
blob: 03d48911459a66ff0d962f31d017eb1158660bb7 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Reflection;
using System.Text.RegularExpressions;

namespace StardewModdingAPI.Internal
{
    /// <summary>Provides extension methods for handling exceptions.</summary>
    internal static class ExceptionHelper
    {
        /*********
        ** 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)
        {
            try
            {
                string message;
                switch (exception)
                {
                    case TypeLoadException ex:
                        message = $"Failed loading type '{ex.TypeName}': {exception}";
                        break;

                    case ReflectionTypeLoadException ex:
                        string summary = ex.ToString();
                        foreach (Exception childEx in ex.LoaderExceptions ?? Array.Empty<Exception>())
                            summary += $"\n\n{childEx?.GetLogSummary()}";
                        message = summary;
                        break;

                    default:
                        message = exception?.ToString() ?? $"<null exception>\n{Environment.StackTrace}";
                        break;
                }

                return ExceptionHelper.SimplifyExtensionMessage(message);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Failed handling {exception?.GetType().FullName} (original message: {exception?.Message})", ex);
            }
        }

        /// <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;
        }

        /// <summary>Simplify common patterns in exception log messages that don't convey useful info.</summary>
        /// <param name="message">The log message to simplify.</param>
        public static string SimplifyExtensionMessage(string message)
        {
            // remove namespace for core exception types
            message = Regex.Replace(
                message,
                @"(?:StardewModdingAPI\.Framework\.Exceptions|Microsoft\.Xna\.Framework|System|System\.IO)\.([a-zA-Z]+Exception):",
                "$1:"
            );

            // remove unneeded root build paths for SMAPI and Stardew Valley
            message = message
                .Replace(@"E:\source\_Stardew\SMAPI\src\", "")
                .Replace(@"C:\GitlabRunner\builds\Gq5qA5P4\0\ConcernedApe\", "");

            // remove placeholder info in Linux/macOS stack traces
            return message
                .Replace(@"<filename unknown>:0", "");
        }
    }
}