From 5e16ed0eea2cae21badd525afa0d464700bb8647 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 5 Aug 2021 14:28:29 -0400 Subject: [PATCH] prevent weird null reference exception in error-handling --- docs/release-notes.md | 4 ++++ src/SMAPI.Internal/ExceptionExtensions.cs | 27 ++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/docs/release-notes.md b/docs/release-notes.md index 20fdd2c1..8c98af5d 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1,6 +1,10 @@ ← [README](README.md) # Release notes +## Upcoming release +* For mod authors: + * Fixed rare `NullReferenceException` in SMAPI's error-handling. + ## 3.12.2 Released 05 August 2021 for Stardew Valley 1.5.4 or later. diff --git a/src/SMAPI.Internal/ExceptionExtensions.cs b/src/SMAPI.Internal/ExceptionExtensions.cs index 5f1ee1fa..d8189048 100644 --- a/src/SMAPI.Internal/ExceptionExtensions.cs +++ b/src/SMAPI.Internal/ExceptionExtensions.cs @@ -13,19 +13,26 @@ namespace StardewModdingAPI.Internal /// The error to summarize. public static string GetLogSummary(this Exception exception) { - switch (exception) + try { - case TypeLoadException ex: - return $"Failed loading type '{ex.TypeName}': {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; + 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(); + default: + return exception?.ToString() ?? $"\n{Environment.StackTrace}"; + } + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed handling {exception?.GetType().FullName} (original message: {exception?.Message})", ex); } }