From 6ac5ca090da088e96b0c6508866cebe7eedc439d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 18 Sep 2021 12:43:31 -0400 Subject: simplify exception logs --- docs/release-notes.md | 3 +- src/SMAPI.Internal/ExceptionExtensions.cs | 48 ------------------ src/SMAPI.Internal/ExceptionHelper.cs | 76 +++++++++++++++++++++++++++++ src/SMAPI.Internal/SMAPI.Internal.projitems | 2 +- src/SMAPI/Framework/Logging/LogManager.cs | 4 ++ 5 files changed, 83 insertions(+), 50 deletions(-) delete mode 100644 src/SMAPI.Internal/ExceptionExtensions.cs create mode 100644 src/SMAPI.Internal/ExceptionHelper.cs diff --git a/docs/release-notes.md b/docs/release-notes.md index f330c677..f619623f 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -4,7 +4,8 @@ ## Upcoming release * For players: * Added more progress updates in the log during startup. - * Simplified common asset load error message. + * Simplified asset load error message. + * Simplified exception logs. * Fixed crash loading mods with corrupted translation files. * For mod authors: diff --git a/src/SMAPI.Internal/ExceptionExtensions.cs b/src/SMAPI.Internal/ExceptionExtensions.cs deleted file mode 100644 index d8189048..00000000 --- a/src/SMAPI.Internal/ExceptionExtensions.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Reflection; - -namespace StardewModdingAPI.Internal -{ - /// Provides extension methods for handling exceptions. - internal static class ExceptionExtensions - { - /********* - ** Public methods - *********/ - /// Get a string representation of an exception suitable for writing to the error log. - /// The error to summarize. - public static string GetLogSummary(this Exception exception) - { - try - { - 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() ?? $"\n{Environment.StackTrace}"; - } - } - catch (Exception ex) - { - throw new InvalidOperationException($"Failed handling {exception?.GetType().FullName} (original message: {exception?.Message})", ex); - } - } - - /// Get the lowest exception in an exception stack. - /// The exception from which to search. - public static Exception GetInnermostException(this Exception exception) - { - while (exception.InnerException != null) - exception = exception.InnerException; - return exception; - } - } -} diff --git a/src/SMAPI.Internal/ExceptionHelper.cs b/src/SMAPI.Internal/ExceptionHelper.cs new file mode 100644 index 00000000..4bc55f95 --- /dev/null +++ b/src/SMAPI.Internal/ExceptionHelper.cs @@ -0,0 +1,76 @@ +using System; +using System.Reflection; +using System.Text.RegularExpressions; + +namespace StardewModdingAPI.Internal +{ + /// Provides extension methods for handling exceptions. + internal static class ExceptionHelper + { + /********* + ** Public methods + *********/ + /// Get a string representation of an exception suitable for writing to the error log. + /// The error to summarize. + 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 ?? new Exception[0]) + summary += $"\n\n{childEx?.GetLogSummary()}"; + message = summary; + break; + + default: + message = exception?.ToString() ?? $"\n{Environment.StackTrace}"; + break; + } + + return ExceptionHelper.SimplifyExtensionMessage(message); + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed handling {exception?.GetType().FullName} (original message: {exception?.Message})", ex); + } + } + + /// Get the lowest exception in an exception stack. + /// The exception from which to search. + public static Exception GetInnermostException(this Exception exception) + { + while (exception.InnerException != null) + exception = exception.InnerException; + return exception; + } + + /// Simplify common patterns in exception log messages that don't convey useful info. + /// The log message to simplify. + 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(@"C:\source\_Stardew\SMAPI\src\", "") + .Replace(@"C:\GitlabRunner\builds\Gq5qA5P4\0\ConcernedApe\", ""); + + // remove placeholder info in Linux/macOS stack traces + return message + .Replace(@":0", ""); + } + } +} diff --git a/src/SMAPI.Internal/SMAPI.Internal.projitems b/src/SMAPI.Internal/SMAPI.Internal.projitems index 0ee94a5b..41d356c0 100644 --- a/src/SMAPI.Internal/SMAPI.Internal.projitems +++ b/src/SMAPI.Internal/SMAPI.Internal.projitems @@ -14,6 +14,6 @@ - + \ No newline at end of file diff --git a/src/SMAPI/Framework/Logging/LogManager.cs b/src/SMAPI/Framework/Logging/LogManager.cs index 6fe44d98..f2876146 100644 --- a/src/SMAPI/Framework/Logging/LogManager.cs +++ b/src/SMAPI/Framework/Logging/LogManager.cs @@ -406,6 +406,10 @@ namespace StardewModdingAPI.Framework.Logging } } + // simplify exception messages + if (level == LogLevel.Error) + message = ExceptionHelper.SimplifyExtensionMessage(message); + // forward to monitor gameMonitor.Log(message, level); } -- cgit