Example #1
0
        /// <summary>
        /// Surrounds an action by protected scope: any exception thrown by this action gets wrapped in a CallGuardException.
        /// If action is unassigned, nothing is done
        /// </summary>
        /// <remarks>
        /// You can use another version of this function with action argument in order not to create unneeded closures
        /// </remarks>
        public static void Protect(Action action,
                                   [CallerFilePath]   string callFile   = null,
                                   [CallerLineNumber] int callLine      = 0,
                                   [CallerMemberName] string callMember = null)
        {
            if (action == null)
            {
                return;
            }

            try
            {
                action();
            }
            catch (Exception error)
            {
                var callSite = GuardUtils.callSiteOf(callFile, callLine, callMember);
                throw new CallGuardException(callSite,
                                             nameof(action),
                                             StringConsts.GUARDED_ACTION_SCOPE_ERROR
                                             .Args(callSite ?? CoreConsts.UNKNOWN, error.ToMessageWithType()),
                                             error);
            }
        }
Example #2
0
        /// <summary>
        /// Surrounds an action by protected scope: any exception thrown by this action gets wrapped in a CallGuardException.
        /// If action is unassigned, nothing is done
        /// </summary>
        /// <remarks>
        /// Use this function with action argument not to create unneeded closures
        /// </remarks>
        public static async Task ProtectAsync <TArg>(TArg arg, Func <TArg, Task> action,
                                                     [CallerFilePath]   string callFile   = null,
                                                     [CallerLineNumber] int callLine      = 0,
                                                     [CallerMemberName] string callMember = null)
        {
            if (action == null)
            {
                return;
            }

            try
            {
                await action(arg).ConfigureAwait(false);
            }
            catch (Exception error)
            {
                var callSite = GuardUtils.callSiteOf(callFile, callLine, callMember);
                throw new CallGuardException(callSite,
                                             nameof(action),
                                             StringConsts.GUARDED_ACTION_SCOPE_ERROR
                                             .Args(callSite ?? CoreConsts.UNKNOWN, error.ToMessageWithType()),
                                             error);
            }
        }