prevent weird null reference exception in error-handling
This commit is contained in:
parent
94d41cd67a
commit
5e16ed0eea
|
@ -1,6 +1,10 @@
|
||||||
← [README](README.md)
|
← [README](README.md)
|
||||||
|
|
||||||
# Release notes
|
# Release notes
|
||||||
|
## Upcoming release
|
||||||
|
* For mod authors:
|
||||||
|
* Fixed rare `NullReferenceException` in SMAPI's error-handling.
|
||||||
|
|
||||||
## 3.12.2
|
## 3.12.2
|
||||||
Released 05 August 2021 for Stardew Valley 1.5.4 or later.
|
Released 05 August 2021 for Stardew Valley 1.5.4 or later.
|
||||||
|
|
||||||
|
|
|
@ -13,19 +13,26 @@ namespace StardewModdingAPI.Internal
|
||||||
/// <param name="exception">The error to summarize.</param>
|
/// <param name="exception">The error to summarize.</param>
|
||||||
public static string GetLogSummary(this Exception exception)
|
public static string GetLogSummary(this Exception exception)
|
||||||
{
|
{
|
||||||
switch (exception)
|
try
|
||||||
{
|
{
|
||||||
case TypeLoadException ex:
|
switch (exception)
|
||||||
return $"Failed loading type '{ex.TypeName}': {exception}";
|
{
|
||||||
|
case TypeLoadException ex:
|
||||||
|
return $"Failed loading type '{ex.TypeName}': {exception}";
|
||||||
|
|
||||||
case ReflectionTypeLoadException ex:
|
case ReflectionTypeLoadException ex:
|
||||||
string summary = ex.ToString();
|
string summary = ex.ToString();
|
||||||
foreach (Exception childEx in ex.LoaderExceptions ?? new Exception[0])
|
foreach (Exception childEx in ex.LoaderExceptions ?? new Exception[0])
|
||||||
summary += $"\n\n{childEx?.GetLogSummary()}";
|
summary += $"\n\n{childEx?.GetLogSummary()}";
|
||||||
return summary;
|
return summary;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return exception.ToString();
|
return exception?.ToString() ?? $"<null exception>\n{Environment.StackTrace}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Failed handling {exception?.GetType().FullName} (original message: {exception?.Message})", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue