internal static void HandleException(Exception exception, ModLogger logger = null, LogType type = LogType.CRITICAL) { if (handledException != null) { return; } string message = $"{(!exception.Message.Matches(@"^(?:(?!Exception:).)*Exception:(?!.*Exception:).*$") ? exception.GetType().Name + ": " : string.Empty)}{ReplaceInnerIfPresent(exception.Message, exception.InnerException)}"; if (!string.IsNullOrWhiteSpace(exception.StackTrace)) { message += $"\n{StackTracing.ParseStackTrace(new StackTrace(exception, true)).TrimEnd('\n')}"; } (logger ?? GuuCore.LOGGER)?.Log(type, message); File.Copy(UNITY_LOG_FILE, NEW_UNITY_LOG_FILE, true); if (type == LogType.CRITICAL) { handledException = exception; reportWindow.Open(); } }
/// <summary> /// Handles a thrown exception /// </summary> /// <param name="func">The function that could lead to the exception</param> /// <param name="logger">The logger to print the handled exception, null to use Guu's logger</param> public static T HandleThrow <T>(Func <T> func, ModLogger logger = null) { try { return(func.Invoke()); } catch (Exception e) { HandleException(e, logger); } return(default);
/// <summary> /// Handles a thrown exception /// </summary> /// <param name="action">The action that could lead to the exception</param> /// <param name="logger">The logger to print the handled exception, null to use Guu's logger</param> public static void HandleThrow(Action action, ModLogger logger = null) { try { action.Invoke(); } catch (Exception e) { HandleException(e, logger); } }