Пример #1
0
        /// <summary>
        /// Creates a new <see cref="ExceptionCallHandler"/> that processses exceptions
        /// using the given exception policy name, as defined in <paramref name="configurationSource"/>.
        /// </summary>
        /// <param name="exceptionPolicyName">Exception policy name.</param>
        /// <param name="configurationSource">Configuration source defining the exception handling policy.</param>
        public ExceptionCallHandler(string exceptionPolicyName, IConfigurationSource configurationSource)
        {
            this.exceptionPolicyName = exceptionPolicyName;
            ExceptionPolicyFactory policyFactory = new ExceptionPolicyFactory(configurationSource);

            exceptionPolicy = policyFactory.Create(exceptionPolicyName);
        }
        public void ThenNewlyRetrievedPolicyReflectsNewConfiguration()
        {
            ExceptionPolicyImpl exceptionPolicy = container.Resolve <ExceptionPolicyImpl>(exceptionPolicy1.Name);
            bool notifyRetrow = exceptionPolicy.HandleException(new Exception());

            Assert.AreEqual(true, notifyRetrow);
        }
Пример #3
0
        public void CanGetExceptionMessageFromResource()
        {
            const string        resourceName        = "ExceptionMessage";
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");

            settings.ExceptionPolicies.Add(exceptionPolicyData);
            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);

            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);
            WrapHandlerData exceptionHandlerData = new WrapHandlerData("handler1", "wrapped", typeof(ArgumentException).AssemblyQualifiedName);

            exceptionHandlerData.ExceptionMessageResourceName = resourceName;
            exceptionHandlerData.ExceptionMessageResourceType = typeof(Resources).AssemblyQualifiedName;
            string resourceValue = Resources.ExceptionMessage;

            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);
            container.AddExtension(new ExceptionHandlingBlockExtension());
            ExceptionPolicyImpl policy            = container.Resolve <ExceptionPolicyImpl>("policy");
            Exception           originalException = new Exception("to be wrapped");

            try
            {
                policy.HandleException(originalException);
                Assert.Fail("a new exception should have been thrown");
            }
            catch (ArgumentException e)
            {
                Assert.AreEqual(resourceValue, e.Message);
                Assert.AreSame(originalException, e.InnerException);
            }
        }
Пример #4
0
        public void CanCreatePoliciesForHandler()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");

            settings.ExceptionPolicies.Add(exceptionPolicyData);
            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);

            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);
            CustomHandlerData exceptionHandlerData = new CustomHandlerData("handler1", typeof(TestCustomExceptionHandler));

            exceptionHandlerData.Attributes.Add(TestCustomExceptionHandler.AttributeKey, "custom handler");
            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);
            container.AddExtension(new ExceptionHandlingBlockExtension());
            ExceptionPolicyImpl policy            = container.Resolve <ExceptionPolicyImpl>("policy");
            Exception           originalException = new Exception("to be replaced");

            try
            {
                policy.HandleException(originalException);
                Assert.Fail("a new exception should have been thrown");
            }
            catch (Exception e)
            {
                Assert.AreEqual("custom handler", e.Message);
                Assert.AreSame(originalException, e.InnerException);
                Assert.AreSame(originalException, TestCustomExceptionHandler.handledException);
            }
        }
        public void CanCreatePolicyWithEmptyTypes()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");

            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData1
                = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);

            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData1);
            ExceptionTypeData exceptionTypeData2
                = new ExceptionTypeData("type2", typeof(ArgumentException), PostHandlingAction.NotifyRethrow);

            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData2);

            container.AddExtension(new ExceptionHandlingBlockExtension());

            ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("policy");

            Assert.IsNotNull(policy);
            Assert.IsNotNull(policy.GetPolicyEntry(typeof(Exception)));
            Assert.IsNotNull(policy.GetPolicyEntry(typeof(ArgumentException)));
            Assert.IsNull(policy.GetPolicyEntry(typeof(InvalidOperationException)));

            // little detail is exposed for policy entries - need to probe its behavior for a proper assert
            Assert.IsFalse(policy.GetPolicyEntry(typeof(Exception)).Handle(new Exception()));
            Assert.IsTrue(policy.GetPolicyEntry(typeof(ArgumentException)).Handle(new ArgumentException()));
        }
		public void HandlerInChainThrowsExceptionWhenProduceError()
		{
			ExceptionPolicyData policyData = PolicyData;
			Dictionary<Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
			ExceptionPolicyImpl policyIml = new ExceptionPolicyImpl(policyData.Name, entries);
			policyIml.HandleException(new ArgumentException());
		}		
		public void HandleExceptionWithNullExceptionThrows()
		{
			ExceptionPolicyData policyData = PolicyData;
			Dictionary<Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
			ExceptionPolicyImpl policyIml = new ExceptionPolicyImpl(policyData.Name, entries);
			policyIml.HandleException(null);			
		}
        public void CanCreatePolicyWithSimpleExceptionHandler()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");

            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);

            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);

            ExceptionHandlerData exceptionHandlerData = new WrapHandlerData("handler1", "message",
                                                                            typeof(ArgumentException).AssemblyQualifiedName);

            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);

            container.AddExtension(new ExceptionHandlingBlockExtension());

            ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("policy");

            try
            {
                policy.HandleException(new Exception("to be wrapped"));
                Assert.Fail("a new exception should have been thrown");
            }
            catch (ArgumentException e)
            {
                Assert.AreEqual("message", e.Message);
            }
        }
Пример #9
0
        public void CanCreatePoliciesForHandler()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");

            settings.ExceptionPolicies.Add(exceptionPolicyData);
            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);

            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);
            FaultContractExceptionHandlerData exceptionHandlerData = new FaultContractExceptionHandlerData("handler1",
                                                                                                           typeof(MockFaultContract).AssemblyQualifiedName);

            exceptionHandlerData.ExceptionMessage = "fault message";
            exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("Message", "{Message}"));
            exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("Data", "{Data}"));
            exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("SomeNumber", "{OffendingNumber}"));
            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);
            container.AddExtension(new ExceptionHandlingBlockExtension());
            ExceptionPolicyImpl      policy            = container.Resolve <ExceptionPolicyImpl>("policy");
            NotFiniteNumberException originalException = new NotFiniteNumberException("MyException", 12341234123412);

            originalException.Data.Add("someKey", "someValue");
            try
            {
                policy.HandleException(originalException);
                Assert.Fail("a new exception should have been thrown");
            }
            catch (FaultContractWrapperException e)
            {
                MockFaultContract fault = (MockFaultContract)e.FaultContract;
                Assert.AreEqual(originalException.Message, fault.Message);
                Assert.AreEqual(originalException.Data.Count, fault.Data.Count);
                Assert.AreEqual(originalException.Data["someKey"], fault.Data["someKey"]);
                Assert.AreEqual(originalException.OffendingNumber, fault.SomeNumber);
            }
        }
Пример #10
0
        public void HandleExceptionWithNullExceptionThrows()
        {
            ExceptionPolicyData policyData = PolicyData;
            Dictionary <Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
            ExceptionPolicyImpl policyIml = new ExceptionPolicyImpl(policyData.Name, entries);

            policyIml.HandleException(null);
        }
Пример #11
0
        public void HandlerInChainThrowsExceptionWhenProduceError()
        {
            ExceptionPolicyData policyData = PolicyData;
            Dictionary <Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
            ExceptionPolicyImpl policyIml = new ExceptionPolicyImpl(policyData.Name, entries);

            policyIml.HandleException(new ArgumentException());
        }
		public void HandleExceptionThatHasNoEntryReturnsTrue()
		{
			ExceptionPolicyData policyData = PolicyData;
			Dictionary<Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
			ExceptionPolicyImpl policyIml = new ExceptionPolicyImpl(policyData.Name, entries);
			bool handled = policyIml.HandleException(new InvalidCastException());
			Assert.IsTrue(handled);
		}
Пример #13
0
        public void HandleExceptionThatHasNoEntryReturnsTrue()
        {
            ExceptionPolicyData policyData = PolicyData;
            Dictionary <Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
            ExceptionPolicyImpl policyIml = new ExceptionPolicyImpl(policyData.Name, entries);
            bool handled = policyIml.HandleException(new InvalidCastException());

            Assert.IsTrue(handled);
        }
Пример #14
0
        public void CanGetExceptionMessageFromResourceForDifferentLocales()
        {
            const string        resourceName        = "ExceptionMessage";
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");

            settings.ExceptionPolicies.Add(exceptionPolicyData);
            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);

            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);
            WrapHandlerData exceptionHandlerData = new WrapHandlerData("handler1", "wrapped", typeof(ArgumentException).AssemblyQualifiedName);

            exceptionHandlerData.ExceptionMessageResourceName = resourceName;
            exceptionHandlerData.ExceptionMessageResourceType = typeof(Resources).AssemblyQualifiedName;
            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);
            container.AddExtension(new ExceptionHandlingBlockExtension());
            ExceptionPolicyImpl policy            = container.Resolve <ExceptionPolicyImpl>("policy");
            Exception           originalException = new Exception("to be wrapped");
            string      enMessage        = null;
            string      esMessage        = null;
            CultureInfo currentUICulture = CultureInfo.CurrentUICulture;

            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
                try
                {
                    policy.HandleException(originalException);
                    Assert.Fail("a new exception should have been thrown");
                }
                catch (ArgumentException e)
                {
                    Assert.AreEqual(Resources.ExceptionMessage, e.Message);
                    Assert.AreSame(originalException, e.InnerException);
                    enMessage = e.Message;
                }
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("es");
                try
                {
                    policy.HandleException(originalException);
                    Assert.Fail("a new exception should have been thrown");
                }
                catch (ArgumentException e)
                {
                    Assert.AreEqual(Resources.ExceptionMessage, e.Message);
                    Assert.AreSame(originalException, e.InnerException);
                    esMessage = e.Message;
                }
                Assert.AreNotEqual(enMessage, esMessage);
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = currentUICulture;
            }
        }
Пример #15
0
        public void WhenRegisteredAgainstExceptionType_OnlyCorrectHandlerIsCalled()
        {
            ExceptionPolicyImpl policyImpl = new ExceptionPolicyImpl("APolicyName",
                                                                     new List <ExceptionPolicyEntry>()
            {
                entry1, entry2
            });

            policyImpl.HandleException(new ArgumentNullException("TestException"));
            Assert.AreEqual(1, handler1.instanceHandledExceptionCount);
            Assert.AreEqual(0, handler2.instanceHandledExceptionCount);
        }
        public void CanCreateEmptyPolicy()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");

            settings.ExceptionPolicies.Add(exceptionPolicyData);

            using (var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("policy");
                Assert.IsNotNull(policy);
            }
        }
        public void CanCreateEmptyPolicy()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");

            settings.ExceptionPolicies.Add(exceptionPolicyData);

            container.AddExtension(new ExceptionHandlingBlockExtension());

            ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("policy");

            Assert.IsNotNull(policy);
        }
Пример #18
0
        /// <summary>
        /// This method handle the policy by enterprise library exception handling block
        /// </summary>
        /// <param name="policyName">Policy Name.</param>
        /// <param name="ex">Exception</param>
        /// <returns></returns>
        public bool Handle(string policyName, ref Exception ex)
        {
            bool rethrow = false;
            ExceptionPolicyImpl exceptImpl = _exceptionPolicyFactory.Create(policyName);

            rethrow = exceptImpl.HandleException(ex);
            if (rethrow)
            {
                throw ex;
            }
            return(rethrow);
        }
        public void ExceptionHandledThroughLoggingBlock()
        {
            MockTraceListener.Reset();

            ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("Logging Policy");

            Assert.IsFalse(policy.HandleException(new Exception("TEST EXCEPTION")));

            Assert.AreEqual(1, MockTraceListener.LastEntry.Categories.Count);
            Assert.IsTrue(MockTraceListener.LastEntry.Categories.Contains("TestCat"));
            Assert.AreEqual(5, MockTraceListener.LastEntry.EventId);
            Assert.AreEqual(TraceEventType.Error, MockTraceListener.LastEntry.Severity);
            Assert.AreEqual("TestTitle", MockTraceListener.LastEntry.Title);
        }
        public void ShouldCreateHandlerWithCorrectPolicy()
        {
            string policyName = "Swallow Exceptions";
            ExceptionCallHandlerAttribute attribute = new ExceptionCallHandlerAttribute(policyName);
            attribute.Order = 400;
            
            IUnityContainer container = new UnityContainer();
            var policy = new ExceptionPolicyImpl(policyName, new ExceptionPolicyEntry[0]);
            container.RegisterInstance(policyName, policy);

            ExceptionCallHandler handler = (ExceptionCallHandler)attribute.CreateHandler(container);
            Assert.AreSame(policy, handler.ExceptionPolicy);
            Assert.AreEqual(400, handler.Order);
        }
        public void SetUp()
        {
            IExceptionHandler[] handlers = new IExceptionHandler[] { new MockThrowingExceptionHandler() };
            ExceptionPolicyEntry policyEntry = new ExceptionPolicyEntry(PostHandlingAction.None, handlers);
            Dictionary<Type, ExceptionPolicyEntry> policyEntries = new Dictionary<Type,ExceptionPolicyEntry>();
            policyEntries.Add(typeof(ArgumentException), policyEntry);

            exceptionPolicy = new ExceptionPolicyImpl(policyName, policyEntries);

            ReflectionInstrumentationAttacher attacher
                = new ReflectionInstrumentationAttacher(
                    exceptionPolicy.GetInstrumentationEventProvider(),
                    typeof(ExceptionHandlingInstrumentationListener),
                    new object[] { policyName, true, true, true });
            attacher.BindInstrumentation();
        }
        public void ShouldCreateHandlerWithCorrectPolicy()
        {
            string policyName = "Swallow Exceptions";
            ExceptionCallHandlerAttribute attribute = new ExceptionCallHandlerAttribute(policyName);

            attribute.Order = 400;

            IUnityContainer container = new UnityContainer();
            var             policy    = new ExceptionPolicyImpl(policyName, new ExceptionPolicyEntry[0]);

            container.RegisterInstance(policyName, policy);

            ExceptionCallHandler handler = (ExceptionCallHandler)attribute.CreateHandler(container);

            Assert.AreSame(policy, handler.ExceptionPolicy);
            Assert.AreEqual(400, handler.Order);
        }
        public void SetUp()
        {
            IExceptionHandler[]  handlers    = new IExceptionHandler[] { new MockThrowingExceptionHandler() };
            ExceptionPolicyEntry policyEntry = new ExceptionPolicyEntry(PostHandlingAction.None, handlers);
            Dictionary <Type, ExceptionPolicyEntry> policyEntries = new Dictionary <Type, ExceptionPolicyEntry>();

            policyEntries.Add(typeof(ArgumentException), policyEntry);

            exceptionPolicy = new ExceptionPolicyImpl(policyName, policyEntries);

            ReflectionInstrumentationAttacher attacher
                = new ReflectionInstrumentationAttacher(
                      exceptionPolicy.GetInstrumentationEventProvider(),
                      typeof(ExceptionHandlingInstrumentationListener),
                      new object[] { policyName, true, true, true });

            attacher.BindInstrumentation();
        }
        public void PoliciesForExceptionPoliciesCanHandleRepeatedTypesWithRepeatedHandlers()
        {
            ExceptionPolicyData exceptionPolicy1Data = new ExceptionPolicyData("policy1");

            settings.ExceptionPolicies.Add(exceptionPolicy1Data);

            ExceptionTypeData exceptionTypeData11
                = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);

            exceptionPolicy1Data.ExceptionTypes.Add(exceptionTypeData11);
            ExceptionHandlerData exceptionHandlerData11 = new WrapHandlerData("handler1", "message1",
                                                                              typeof(ArgumentException).AssemblyQualifiedName);

            exceptionTypeData11.ExceptionHandlers.Add(exceptionHandlerData11);

            ExceptionPolicyData exceptionPolicy2Data = new ExceptionPolicyData("policy2");

            settings.ExceptionPolicies.Add(exceptionPolicy2Data);

            ExceptionTypeData exceptionTypeData21
                = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);

            exceptionPolicy2Data.ExceptionTypes.Add(exceptionTypeData21);
            ExceptionHandlerData exceptionHandlerData21 = new WrapHandlerData("handler1", "message2",
                                                                              typeof(ArgumentException).AssemblyQualifiedName);

            exceptionTypeData21.ExceptionHandlers.Add(exceptionHandlerData21);

            using (var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("policy1");

                try
                {
                    policy.HandleException(new Exception("to be wrapped"));
                    Assert.Fail("a new exception should have been thrown");
                }
                catch (ArgumentException e)
                {
                    Assert.AreEqual("message1", e.Message, "Policy 1 is using the handler definitions from policy 2");
                }
            }
        }
        public void PoliciesForExceptionPoliciesCanHandleRepeatedTypes()
        {
            ExceptionPolicyData exceptionPolicy1Data = new ExceptionPolicyData("policy1");

            settings.ExceptionPolicies.Add(exceptionPolicy1Data);

            ExceptionTypeData exceptionTypeData11
                = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);

            exceptionPolicy1Data.ExceptionTypes.Add(exceptionTypeData11);
            ExceptionTypeData exceptionTypeData12
                = new ExceptionTypeData("type2", typeof(ArgumentException), PostHandlingAction.NotifyRethrow);

            exceptionPolicy1Data.ExceptionTypes.Add(exceptionTypeData12);

            ExceptionPolicyData exceptionPolicy2Data = new ExceptionPolicyData("policy2");

            settings.ExceptionPolicies.Add(exceptionPolicy2Data);

            ExceptionTypeData exceptionTypeData21
                = new ExceptionTypeData("type1", typeof(InvalidOperationException), PostHandlingAction.NotifyRethrow);

            exceptionPolicy2Data.ExceptionTypes.Add(exceptionTypeData21);
            ExceptionTypeData exceptionTypeData22
                = new ExceptionTypeData("type2", typeof(ArgumentNullException), PostHandlingAction.NotifyRethrow);

            exceptionPolicy2Data.ExceptionTypes.Add(exceptionTypeData22);

            using (var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("policy1");

                Assert.IsNotNull(policy);
                Assert.IsNotNull(policy.GetPolicyEntry(typeof(Exception)));
                Assert.IsNotNull(policy.GetPolicyEntry(typeof(ArgumentException)));
                Assert.IsNull(policy.GetPolicyEntry(typeof(InvalidOperationException)));

                // this breaks if the type1 for policy2 overrides the policy for policy1's type
                Assert.IsFalse(policy.GetPolicyEntry(typeof(Exception)).Handle(new Exception()));
                Assert.IsTrue(policy.GetPolicyEntry(typeof(ArgumentException)).Handle(new ArgumentException()));
            }
        }
        public void SetUp()
        {
            var handlers = new IExceptionHandler[] { new MockThrowingExceptionHandler() };

            var instrumentationProvider = new ExceptionHandlingInstrumentationProvider(policyName, true, true,
                                                                                          "ApplicationInstanceName");
            var policyEntry = new ExceptionPolicyEntry(typeof(ArgumentException), PostHandlingAction.None, handlers, instrumentationProvider);
            var policyEntries = new Dictionary<Type, ExceptionPolicyEntry>
            {
                {typeof (ArgumentException), policyEntry}
            };

            exceptionPolicy = new ExceptionPolicyImpl(policyName, policyEntries);

            nameFormatter = new FixedPrefixNameFormatter("Prefix - ");
            provider = new ExceptionHandlingInstrumentationProvider(instanceName, true, true, nameFormatter);
            formattedInstanceName = nameFormatter.CreateName(instanceName);
            totalExceptionHandlersExecuted = new EnterpriseLibraryPerformanceCounter(counterCategoryName, TotalExceptionHandlersExecuted, formattedInstanceName);
            totalExceptionsHandled = new EnterpriseLibraryPerformanceCounter(counterCategoryName, TotalExceptionsHandled, formattedInstanceName);
        }
        public void SetUp()
        {
            var handlers = new IExceptionHandler[] { new MockThrowingExceptionHandler() };

            var instrumentationProvider = new ExceptionHandlingInstrumentationProvider(policyName, true, true,
                                                                                       "ApplicationInstanceName");
            var policyEntry   = new ExceptionPolicyEntry(typeof(ArgumentException), PostHandlingAction.None, handlers, instrumentationProvider);
            var policyEntries = new Dictionary <Type, ExceptionPolicyEntry>
            {
                { typeof(ArgumentException), policyEntry }
            };

            exceptionPolicy = new ExceptionPolicyImpl(policyName, policyEntries);

            nameFormatter                  = new FixedPrefixNameFormatter("Prefix - ");
            provider                       = new ExceptionHandlingInstrumentationProvider(instanceName, true, true, nameFormatter);
            formattedInstanceName          = nameFormatter.CreateName(instanceName);
            totalExceptionHandlersExecuted = new EnterpriseLibraryPerformanceCounter(counterCategoryName, TotalExceptionHandlersExecuted, formattedInstanceName);
            totalExceptionsHandled         = new EnterpriseLibraryPerformanceCounter(counterCategoryName, TotalExceptionsHandled, formattedInstanceName);
        }
        public void SetUp()
        {
            IExceptionHandler[]  handlers    = new IExceptionHandler[] { new MockThrowingExceptionHandler() };
            ExceptionPolicyEntry policyEntry = new ExceptionPolicyEntry(PostHandlingAction.None, handlers);
            Dictionary <Type, ExceptionPolicyEntry> policyEntries = new Dictionary <Type, ExceptionPolicyEntry>();

            policyEntries.Add(typeof(ArgumentException), policyEntry);
            exceptionPolicy = new ExceptionPolicyImpl(policyName, policyEntries);
            ReflectionInstrumentationAttacher attacher
                = new ReflectionInstrumentationAttacher(
                      exceptionPolicy.GetInstrumentationEventProvider(),
                      typeof(ExceptionHandlingInstrumentationListener),
                      new object[] { policyName, true, true, true, "ApplicationInstanceName" });

            attacher.BindInstrumentation();
            nameFormatter                  = new FixedPrefixNameFormatter("Prefix - ");
            listener                       = new ExceptionHandlingInstrumentationListener(instanceName, true, true, true, nameFormatter);
            formattedInstanceName          = nameFormatter.CreateName(instanceName);
            totalExceptionHandlersExecuted = new EnterpriseLibraryPerformanceCounter(counterCategoryName, TotalExceptionHandlersExecuted, formattedInstanceName);
            totalExceptionsHandled         = new EnterpriseLibraryPerformanceCounter(counterCategoryName, TotalExceptionsHandled, formattedInstanceName);
        }
        public void MultipleRequestsUseSameLogWriterInstance()
        {
            MockTraceListener.Reset();

            {
                ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("Logging Policy");
                Assert.IsFalse(policy.HandleException(new Exception("TEST EXCEPTION")));
            }
            {
                ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("Logging Policy");
                Assert.IsFalse(policy.HandleException(new Exception("TEST EXCEPTION")));
            }
            {
                ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("Logging Policy");
                Assert.IsFalse(policy.HandleException(new Exception("TEST EXCEPTION")));
            }

            Assert.AreEqual(3, MockTraceListener.Entries.Count);
            Assert.AreEqual(3, MockTraceListener.Instances.Count);
            Assert.AreSame(MockTraceListener.Instances[0], MockTraceListener.Instances[1]);
            Assert.AreSame(MockTraceListener.Instances[0], MockTraceListener.Instances[2]);
        }
        public void WrapperHandlerCanGetExceptionMessageFromResource()
        {
            const string resourceName = "ExceptionMessage";

            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");

            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);

            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);

            WrapHandlerData exceptionHandlerData = new WrapHandlerData("handler1", "message",
                                                                       typeof(ArgumentException).AssemblyQualifiedName);

            exceptionHandlerData.ExceptionMessageResourceName = resourceName;
            exceptionHandlerData.ExceptionMessageResourceType = typeof(Resources).AssemblyQualifiedName;

            string resourceValue = Resources.ExceptionMessage;

            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);

            using (var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionPolicyImpl policy = container.Resolve <ExceptionPolicyImpl>("policy");

                try
                {
                    policy.HandleException(new Exception("to be wrapped"));
                    Assert.Fail("a new exception should have been thrown");
                }
                catch (ArgumentException e)
                {
                    Assert.AreEqual(resourceValue, e.Message);
                }
            }
        }
Пример #31
0
 /// <summary>
 /// Creates a new <see cref="ExceptionCallHandler"/> that processses exceptions
 /// using the given exception policy.
 /// </summary>
 /// <param name="exceptionPolicy">Exception policy.</param>
 public ExceptionCallHandler(ExceptionPolicyImpl exceptionPolicy)
 {
     this.exceptionPolicy = exceptionPolicy;
 }
        public void WhenRegisteredAgainstExceptionType_OnlyCorrectHandlerIsCalled()
        {

            ExceptionPolicyImpl policyImpl = new ExceptionPolicyImpl("APolicyName",
                                                                     new List<ExceptionPolicyEntry>() {entry1, entry2});

            policyImpl.HandleException(new ArgumentNullException("TestException"));
            Assert.AreEqual(1, handler1.instanceHandledExceptionCount);
            Assert.AreEqual(0, handler2.instanceHandledExceptionCount);
        }
Пример #33
0
 /// <summary>
 /// Creates a new <see cref="ExceptionCallHandler"/> that processses exceptions
 /// using the given exception policy.
 /// </summary>
 /// <param name="exceptionPolicy">Exception policy.</param>
 /// <param name="order">Order in which the handler will be executed.</param>
 public ExceptionCallHandler(ExceptionPolicyImpl exceptionPolicy, int order)
     : this(exceptionPolicy)
 {
     this.order = order;
 }