using System; using TechbloxModdingAPI.Events; namespace TechbloxModdingAPI.Utility { public static class ExceptionUtil { /// /// Invokes an event with a null-check. /// /// The event to emit, can be null /// Event sender /// Event arguments /// Type of the event arguments public static void InvokeEvent(EventHandler handler, object sender, T args) { handler?.Invoke(sender, args); } /// /// Wraps the event handler in a try-catch block to avoid propagating exceptions. /// /// The handler to wrap (not null) /// Type of the event arguments /// The wrapped handler public static EventHandler WrapHandler(EventHandler handler) { return (sender, e) => { try { handler(sender, e); } catch (Exception e1) { EventRuntimeException wrappedException = new EventRuntimeException($"EventHandler with arg type {typeof(T).Name} threw an exception", e1); Logging.LogWarning(wrappedException.ToString()); } }; } } }