Skip to content

Commit

Permalink
simplify exception logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathoschild committed Sep 18, 2021
1 parent 8a11774 commit 6ac5ca0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,20 +16,26 @@ public static string GetLogSummary(this Exception exception)
{
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)
{
Expand All @@ -44,5 +51,26 @@ public static Exception GetInnermostException(this Exception exception)
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", "");
}
}
}
2 changes: 1 addition & 1 deletion src/SMAPI.Internal/SMAPI.Internal.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -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>
4 changes: 4 additions & 0 deletions src/SMAPI/Framework/Logging/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ private void HandleConsoleMessage(IMonitor gameMonitor, string message)
}
}

// simplify exception messages
if (level == LogLevel.Error)
message = ExceptionHelper.SimplifyExtensionMessage(message);

// forward to monitor
gameMonitor.Log(message, level);
}
Expand Down

0 comments on commit 6ac5ca0

Please sign in to comment.