summaryrefslogtreecommitdiff
path: root/src/SMAPI.Internal/ExceptionExtensions.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2021-08-01 13:11:51 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2021-08-01 13:11:51 -0400
commit5b31be76dd90711ff475341de8dfdb6e1f50b98a (patch)
treee120bc62af72e94e8e924ba124382d0c6f9304de /src/SMAPI.Internal/ExceptionExtensions.cs
parent8f96a97f070d654764de3b138678d8f62707f485 (diff)
parentd688cdf8c3c852d4b11cdd046d67c4b35443cc95 (diff)
downloadSMAPI-5b31be76dd90711ff475341de8dfdb6e1f50b98a.tar.gz
SMAPI-5b31be76dd90711ff475341de8dfdb6e1f50b98a.tar.bz2
SMAPI-5b31be76dd90711ff475341de8dfdb6e1f50b98a.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Internal/ExceptionExtensions.cs')
-rw-r--r--src/SMAPI.Internal/ExceptionExtensions.cs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/SMAPI.Internal/ExceptionExtensions.cs b/src/SMAPI.Internal/ExceptionExtensions.cs
new file mode 100644
index 00000000..d7a2252b
--- /dev/null
+++ b/src/SMAPI.Internal/ExceptionExtensions.cs
@@ -0,0 +1,41 @@
+using System;
+using System.Reflection;
+
+namespace StardewModdingAPI.Internal
+{
+ /// <summary>Provides extension methods for handling exceptions.</summary>
+ internal static class ExceptionExtensions
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Get a string representation of an exception suitable for writing to the error log.</summary>
+ /// <param name="exception">The error to summarize.</param>
+ public static string GetLogSummary(this Exception exception)
+ {
+ switch (exception)
+ {
+ case TypeLoadException ex:
+ return $"Failed loading type '{ex.TypeName}': {exception}";
+
+ case ReflectionTypeLoadException ex:
+ string summary = exception.ToString();
+ foreach (Exception childEx in ex.LoaderExceptions)
+ summary += $"\n\n{childEx.GetLogSummary()}";
+ return summary;
+
+ default:
+ return exception.ToString();
+ }
+ }
+
+ /// <summary>Get the lowest exception in an exception stack.</summary>
+ /// <param name="exception">The exception from which to search.</param>
+ public static Exception GetInnermostException(this Exception exception)
+ {
+ while (exception.InnerException != null)
+ exception = exception.InnerException;
+ return exception;
+ }
+ }
+}