diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-07-30 00:54:15 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-07-30 00:54:15 -0400 |
commit | 948c800a98f00b1bdcfd05ee6228e3423f9eb465 (patch) | |
tree | b958b3cdb4428fa8788a5698a207a45912923de7 /src/SMAPI.Internal/ExceptionExtensions.cs | |
parent | 4074f697d73f5cac6699836550b144fd0c4e2803 (diff) | |
download | SMAPI-948c800a98f00b1bdcfd05ee6228e3423f9eb465.tar.gz SMAPI-948c800a98f00b1bdcfd05ee6228e3423f9eb465.tar.bz2 SMAPI-948c800a98f00b1bdcfd05ee6228e3423f9eb465.zip |
migrate to the new Harmony patch pattern used in my mods
That improves validation and error-handling.
Diffstat (limited to 'src/SMAPI.Internal/ExceptionExtensions.cs')
-rw-r--r-- | src/SMAPI.Internal/ExceptionExtensions.cs | 41 |
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; + } + } +} |