using System;

namespace Quadarax.Foundation.Core.Exceptions
{
    public static class TryCatchExt
    {
        public static Exception? TryCatch<TResult>(this object owner, Func<TResult>? fnc)
        {
            if (owner == null)
                throw new ArgumentNullException(nameof(owner));
            if (fnc == null)
                return null;
            try
            {
                fnc();
            }
            catch (Exception e)
            {
                return e;
            }

            return null;
        }
        public static Exception? TryCatch(this object owner, Action? fnc)
        {
            if (owner == null)
                throw new ArgumentNullException(nameof(owner));
            if (fnc == null)
                return null;
            try
            {
                fnc();
            }
            catch (Exception e)
            {
                return e;
            }

            return null;
        }
    }
}
