diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-09-18 12:43:31 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-09-18 12:43:31 -0400 |
commit | 6ac5ca090da088e96b0c6508866cebe7eedc439d (patch) | |
tree | 949166e5875c1969491fecb9d2c4d34afab936f8 /src/SMAPI.Internal | |
parent | 8a117744608dfff970855defbffb1f40466ab755 (diff) | |
download | SMAPI-6ac5ca090da088e96b0c6508866cebe7eedc439d.tar.gz SMAPI-6ac5ca090da088e96b0c6508866cebe7eedc439d.tar.bz2 SMAPI-6ac5ca090da088e96b0c6508866cebe7eedc439d.zip |
simplify exception logs
Diffstat (limited to 'src/SMAPI.Internal')
-rw-r--r-- | src/SMAPI.Internal/ExceptionHelper.cs (renamed from src/SMAPI.Internal/ExceptionExtensions.cs) | 36 | ||||
-rw-r--r-- | src/SMAPI.Internal/SMAPI.Internal.projitems | 2 |
2 files changed, 33 insertions, 5 deletions
diff --git a/src/SMAPI.Internal/ExceptionExtensions.cs b/src/SMAPI.Internal/ExceptionHelper.cs index d8189048..4bc55f95 100644 --- a/src/SMAPI.Internal/ExceptionExtensions.cs +++ b/src/SMAPI.Internal/ExceptionHelper.cs @@ -1,10 +1,11 @@ using System; using System.Reflection; +using System.Text.RegularExpressions; namespace StardewModdingAPI.Internal { /// <summary>Provides extension methods for handling exceptions.</summary> - internal static class ExceptionExtensions + internal static class ExceptionHelper { /********* ** Public methods @@ -15,20 +16,26 @@ namespace StardewModdingAPI.Internal { try { + string message; switch (exception) { case TypeLoadException ex: - return $"Failed loading type '{ex.TypeName}': {exception}"; + 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()}"; - return summary; + message = summary; + break; default: - return exception?.ToString() ?? $"<null exception>\n{Environment.StackTrace}"; + message = exception?.ToString() ?? $"<null exception>\n{Environment.StackTrace}"; + break; } + + return ExceptionHelper.SimplifyExtensionMessage(message); } catch (Exception ex) { @@ -44,5 +51,26 @@ namespace StardewModdingAPI.Internal 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(@"C:\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", ""); + } } } 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 @@ <Compile Include="$(MSBuildThisFileDirectory)ConsoleWriting\ConsoleLogLevel.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ConsoleWriting\IConsoleWriter.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ConsoleWriting\MonitorColorScheme.cs" /> - <Compile Include="$(MSBuildThisFileDirectory)ExceptionExtensions.cs" /> + <Compile Include="$(MSBuildThisFileDirectory)ExceptionHelper.cs" /> </ItemGroup> </Project>
\ No newline at end of file |