From 1dc3f1013f9aa654bdcf22cdd22f1efd3d5d2315 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 23 Nov 2016 20:23:42 -0500 Subject: log relevant details when a ReflectionTypeLoadException is caught by SMAPI --- .../Framework/InternalExtensions.cs | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/StardewModdingAPI/Framework') diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs index d08d12f3..71f70fd5 100644 --- a/src/StardewModdingAPI/Framework/InternalExtensions.cs +++ b/src/StardewModdingAPI/Framework/InternalExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; namespace StardewModdingAPI.Framework { @@ -32,7 +33,7 @@ namespace StardewModdingAPI.Framework } catch (Exception ex) { - monitor.Log($"A mod failed handling the {name} event:\n{ex}", LogLevel.Error); + monitor.Log($"A mod failed handling the {name} event:\n{ex.GetLogSummary()}", LogLevel.Error); } } } @@ -57,9 +58,27 @@ namespace StardewModdingAPI.Framework } catch (Exception ex) { - monitor.Log($"A mod failed handling the {name} event:\n{ex}", LogLevel.Error); + monitor.Log($"A mod failed handling the {name} event:\n{ex.GetLogSummary()}", LogLevel.Error); } } } + + /**** + ** Exceptions + ****/ + /// Get a string representation of an exception suitable for writing to the error log. + /// The error to summarise. + public static string GetLogSummary(this Exception exception) + { + string summary = exception.ToString(); + + if (exception is ReflectionTypeLoadException) + { + foreach (Exception childEx in ((ReflectionTypeLoadException)exception).LoaderExceptions) + summary += $"\n\n{childEx.GetLogSummary()}"; + } + + return summary; + } } } -- cgit