using System;
using GamecraftModdingAPI.Events;
namespace GamecraftModdingAPI.Utility
{
public static class ExceptionUtil
{
///
/// Invokes an event in a try-catch block to avoid propagating exceptions.
///
/// 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)
{
try
{
handler?.Invoke(sender, args);
}
catch (Exception e)
{
EventRuntimeException wrappedException =
new EventRuntimeException($"EventHandler with arg type {typeof(T).Name} threw an exception", e);
Logging.LogWarning(wrappedException.ToString());
}
}
}
}