Esempio n. 1
0
 /// <summary>
 /// Builds handler out of strategies
 /// </summary>
 /// <returns></returns>
 public IStrategyHandler Build()
 {
     if (!_strategyHandlers.Any())
     {
         var _handler = new EmptyHandler();
         _handler.AutoHandleErrorProcesses = ShouldAutoHandle;
         return(_handler);
     }
     else if (_strategyHandlers.Count > 1)
     {
         var _handler = new WrapHandler(_strategyHandlers.ToArray());
         _handler.AutoHandleErrorProcesses = ShouldAutoHandle;
         return(_handler);
     }
     else if (_strategyHandlers.Count == 1)
     {
         var _handler = _strategyHandlers.First();
         _handler.AutoHandleErrorProcesses = ShouldAutoHandle;
         return(_handler);
     }
     else
     {
         var _handler = new EmptyHandler();
         _handler.AutoHandleErrorProcesses = ShouldAutoHandle;
         return(_handler);
     }
 }
        public void CanWrapException()
        {
            WrapHandler handler = new WrapHandler(message, typeof(ApplicationException));
            Exception   ex      = handler.HandleException(new InvalidOperationException(), Guid.NewGuid());

            Assert.AreEqual(typeof(ApplicationException), ex.GetType());
            Assert.AreEqual(typeof(ApplicationException), handler.WrapExceptionType);
            Assert.AreEqual(message, ex.Message);
            Assert.AreEqual(typeof(InvalidOperationException), ex.InnerException.GetType());
        }
        public void CanWrapException()
        {
            WrapHandler handler = new WrapHandler(message, typeof(ApplicationException));
            Exception ex = handler.HandleException(new InvalidOperationException(), Guid.NewGuid());

            Assert.AreEqual(typeof(ApplicationException), ex.GetType());
            Assert.AreEqual(typeof(ApplicationException), handler.WrapExceptionType);
            Assert.AreEqual(message, ex.Message);
            Assert.AreEqual(typeof(InvalidOperationException), ex.InnerException.GetType());
        }
        public void then_merge_record_has_appropriate_path_after_name_change()
        {
            OverriddenExceptionMessage.Value   = "new exception message";
            WrapHandler.Property("Name").Value = "New Name";

            var mergeElements = EnvironmentSection.MergeElements.OfType <EnvironmentalOverridesElement>();
            var mergeRecord   = mergeElements.Where(x => x.LogicalParentElementPath == base.WrapHandler.Path).FirstOrDefault();

            Assert.IsNotNull(mergeRecord);
        }
        /// <summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// Builds a <see cref="WrapHandler"/> based on an instance of <see cref="WrapHandlerData"/>.
        /// </summary>
        /// <seealso cref="ExceptionHandlerCustomFactory"/>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="WrapHandlerData"/>.</param>
        /// <param name="configurationSource">The source for configuration objects.</param>
        /// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
        /// <returns>A fully initialized instance of <see cref="WrapHandler"/>.</returns>
        public IExceptionHandler Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            WrapHandlerData castedObjectConfiguration
                = (WrapHandlerData)objectConfiguration;

            WrapHandler createdObject
                = new WrapHandler(castedObjectConfiguration.ExceptionMessage, castedObjectConfiguration.WrapExceptionType);

            return(createdObject);
        }
Esempio n. 6
0
        /// <summary>
        /// Create <see cref="ExceptionPolicyDefinition"/> to implement exception shielding for component.
        /// 1. rethrow all exceptions derived from component base exception <typeparamref name="TBaseException"/>
        /// 2. rethrow <see cref="OperationCanceledException"/>
        /// 3. wrap generic exceptions to component specific exception <typeparamref name="TBaseException"/>
        /// </summary>
        /// <param name="policyDefinitionName">Policy definition name</param>
        /// <param name="wrapExceptionMessage">string resolver to be used in case of wrapping generic exception to component specific exception</param>
        /// <param name="wrapExceptionBeforeHandlers">additional handlers <see cref="IExceptionHandler"/> which will be executed before wrapping generic exception to component specific exception</param>
        /// <param name="additionalPolicyEntries">additional <see cref="ExceptionPolicyEntry"/> which will be added to policy definition before default ones</param>
        /// <typeparam name="TBaseException">Type of base class for component specific exceptions</typeparam>
        /// <returns>Policy definition <see cref="ExceptionPolicyDefinition"/></returns>
        public static ExceptionPolicyDefinition CreateShieldingForComponent <TBaseException>(
            string policyDefinitionName          = null,
            IStringResolver wrapExceptionMessage = null,
            IEnumerable <IExceptionHandler> wrapExceptionBeforeHandlers = null,
            IEnumerable <ExceptionPolicyEntry> additionalPolicyEntries  = null) where TBaseException : Exception
        {
            if (policyDefinitionName == null)
            {
                policyDefinitionName = "ShieldingFor" + typeof(TBaseException).FullName;
            }

            List <IExceptionHandler> handlers = wrapExceptionBeforeHandlers?.ToList() ?? new List <IExceptionHandler>();

            WrapHandler wrapHandler = wrapExceptionMessage == null
                                          ? new WrapHandler(
                "Unhandled exception. See inner for details",
                typeof(TBaseException))
                                          : new WrapHandler(wrapExceptionMessage, typeof(TBaseException));

            handlers.Add(wrapHandler);

            List <ExceptionPolicyEntry> policyEntries = additionalPolicyEntries?.ToList()
                                                        ?? new List <ExceptionPolicyEntry>();

            policyEntries.Add(
                new ExceptionPolicyEntry(
                    typeof(TBaseException),
                    PostHandlingAction.NotifyRethrow,
                    Enumerable.Empty <IExceptionHandler>()));

            policyEntries.Add(
                new ExceptionPolicyEntry(
                    typeof(OperationCanceledException),
                    PostHandlingAction.NotifyRethrow,
                    Enumerable.Empty <IExceptionHandler>()));

            policyEntries.Add(
                new ExceptionPolicyEntry(typeof(Exception), PostHandlingAction.ThrowNewException, handlers));

            ExceptionPolicyDefinition result = new ExceptionPolicyDefinition(policyDefinitionName, policyEntries);

            return(result);
        }
Esempio n. 7
0
    private static void EventsAndStuff()
    {
        WrapHandler wrapHandler1 = new WrapHandler("wrapHandler1");
        WrapHandler wrapHandler2 = new WrapHandler("wrapHandler2");

        SimpleEventAdder simpleAdder = new SimpleEventAdder();

        simpleAdder.OnMultipleOfFiveReached += wrapHandler1.simpleAdder_MultipleOfFiveReached;
        simpleAdder.OnMultipleOfFiveReached += wrapHandler2.simpleAdder_MultipleOfFiveReached;

        int iAnswer = simpleAdder.Add(4, 2);

        Console.WriteLine($"iAnswer = {iAnswer}");
        iAnswer = simpleAdder.Add(4, 6);
        Console.WriteLine($"iAnswer = {iAnswer}");

        wrapHandler2 = null;

        DotNetEventAdder eventAdder = new DotNetEventAdder();

        eventAdder.OnMultipleOfFiveReached += eventAdder_MultipleOfFiveReached;
        iAnswer = eventAdder.Add(4, 2);
        Console.WriteLine($"iAnswer = {iAnswer}");
        iAnswer = eventAdder.Add(4, 6);
        Console.WriteLine($"iAnswer = {iAnswer}");

        try
        {
            iAnswer = eventAdder.Add(short.MaxValue, 1);
            Console.WriteLine($"iAnswer = {iAnswer}");
        }
        catch (MyException e)
        {
            Console.WriteLine($"\"Handled\" MyException: {e.Message}\n{e.StackTrace}");
        }
    }
        public void CreatesWrapExceptionHandlerWithMessageForThrowNewExceptionPostHandlingAction()
        {
            var excepionPolicies      = new List <ExceptionPolicyDefinition>();
            var excepionPolicyEntries = new List <ExceptionPolicyEntry>();

            var exceptionHandler = new WrapHandler("string", typeof(InvalidCastException));
            var exceptionPolicy  = new ExceptionPolicyEntry(typeof(Exception), PostHandlingAction.ThrowNewException, new IExceptionHandler[] { exceptionHandler });

            excepionPolicyEntries.Add(exceptionPolicy);
            excepionPolicies.Add(new ExceptionPolicyDefinition("EH", excepionPolicyEntries));

            var       exceptionManager  = new ExceptionManager(excepionPolicies);
            Exception originalException = new Exception();

            ExceptionAssertHelper.Throws <InvalidCastException>(() => exceptionManager.HandleException(originalException, "EH"));

            Exception exception = null;
            bool      rethrow   = exceptionManager.HandleException(originalException, "EH", out exception);

            Assert.IsInstanceOfType(exception, typeof(InvalidCastException));
            Assert.AreSame(originalException, exception.InnerException);
            Assert.AreEqual("string", exception.Message);
            Assert.IsTrue(rethrow);
        }
Esempio n. 9
0
        /// <summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// Builds a <see cref="WrapHandler"/> based on an instance of <see cref="WrapHandlerData"/>.
        /// </summary>
        /// <seealso cref="ExceptionHandlerCustomFactory"/>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="WrapHandlerData"/>.</param>
        /// <param name="configurationSource">The source for configuration objects.</param>
        /// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
        /// <returns>A fully initialized instance of <see cref="WrapHandler"/>.</returns>
        public IExceptionHandler Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            WrapHandlerData castedObjectConfiguration
                = (WrapHandlerData)objectConfiguration;

            string exceptionMessage = castedObjectConfiguration.ExceptionMessage;

            if (!string.IsNullOrEmpty(castedObjectConfiguration.ExceptionMessageResourceName))
            {
                Type exceptionMessageResourceType = Type.GetType(castedObjectConfiguration.ExceptionMessageResourceType, false);

                if (null != exceptionMessageResourceType)
                {
                    exceptionMessage = ResourceStringLoader.LoadString(exceptionMessageResourceType.FullName,
                                                                       castedObjectConfiguration.ExceptionMessageResourceName,
                                                                       exceptionMessageResourceType.Assembly);
                }
            }

            WrapHandler createdObject
                = new WrapHandler(exceptionMessage, castedObjectConfiguration.WrapExceptionType);

            return(createdObject);
        }
 public void ConstructingWithNullExceptionTypeThrows()
 {
     WrapHandler handler = new WrapHandler(message, null);
 }
        public void HandlerThrowsWhenNotWrappingAnException()
        {
            WrapHandler handler = new WrapHandler(message, typeof(object));

            handler.HandleException(new ApplicationException(), Guid.NewGuid());
        }
 public void ConstructingWithNullExceptionTypeThrows()
 {
     WrapHandler handler = new WrapHandler(message, null);
 }
 public void HandlerThrowsWhenNotWrappingAnException()
 {
     WrapHandler handler = new WrapHandler(message, typeof(object));
     handler.HandleException(new ApplicationException(), Guid.NewGuid());
 }