Beispiel #1
0
        public async void Build()
        {
            var builder = new ExceptionPolicyBuilder(new ServiceCollection().BuildServiceProvider());

            builder.For <FoobarException>(PostHandlingAction.None, handlers => handlers.Use(_ => { _flag += "FoobarException"; return(Task.CompletedTask); }));
            builder.For <FooException>(PostHandlingAction.None, handlers => handlers.Use(_ => { _flag += "FooException"; return(Task.CompletedTask); }));
            builder.For <Exception>(PostHandlingAction.None, handlers => handlers.Use(_ => { _flag += "Exception"; return(Task.CompletedTask); }));
            builder.Pre(handers => handers.Use(_ => { _flag += "Pre"; return(Task.CompletedTask); }));
            builder.Post(handers => handers.Use(_ => { _flag += "Post"; return(Task.CompletedTask); }));
            var policy = builder.Build();

            _flag = "";
            await HandleException <FoobarException>(policy);

            Assert.Equal("PreFoobarExceptionPost", _flag);

            _flag = "";
            await HandleException <FooException>(policy);

            Assert.Equal("PreFooExceptionPost", _flag);

            _flag = "";
            await HandleException <BarException>(policy);

            Assert.Equal("PreFoobarExceptionPost", _flag);


            _flag = "";
            await HandleException <BazException>(policy);

            Assert.Equal("PreExceptionPost", _flag);
        }
Beispiel #2
0
        public void Post_Arguments_Not_Allow_Null(string configureIndicator)
        {
            Action <IExceptionHandlerBuilder> configure = configureIndicator == null
                ? null as Action <IExceptionHandlerBuilder>
                : _ => { };
            var builder = new ExceptionPolicyBuilder(new ServiceCollection().BuildServiceProvider());

            Assert.Throws <ArgumentNullException>(() => builder.Post(configure));
        }
Beispiel #3
0
        public void For_Arguments_Not_Allow_Null(string typeIndicator, string configureIndicator)
        {
            Type exceptionType = typeIndicator == null
                ? null
                : typeof(Exception);
            Action <IExceptionHandlerBuilder> configure = configureIndicator == null
                ? null as Action <IExceptionHandlerBuilder>
                : _ => { };
            var builder = new ExceptionPolicyBuilder(new ServiceCollection().BuildServiceProvider());

            Assert.Throws <ArgumentNullException>(() => builder.For(exceptionType, PostHandlingAction.None, configure));
        }
        public void Pre_Arguments_Not_Allow_Null(string predicateIndicator, string configureIndicator)
        {
            Func <Exception, bool> predicate = predicateIndicator == null
                ? null as Func <Exception, bool>
                : _ => true;
            Action <IExceptionHandlerBuilder> configure = configureIndicator == null
                ? null as Action <IExceptionHandlerBuilder>
                : _ => { };
            var builder = new ExceptionPolicyBuilder(new ServiceCollection().BuildServiceProvider());

            Assert.Throws <ArgumentNullException>(() => builder.Pre(configure, predicate));
        }
        public async void Build()
        {
            var builder = new ExceptionPolicyBuilder(new ServiceCollection().BuildServiceProvider());

            builder.For <FoobarException>(PostHandlingAction.ThrowNew, _ => _.Use(context => { _flag += "Foobar:"; return(Task.CompletedTask); }));
            builder.For <FooException>(PostHandlingAction.ThrowNew, _ => _.Use(context => { _flag += "Foo:"; return(Task.CompletedTask); }));
            builder.For <BarException>(PostHandlingAction.ThrowNew, _ => _.Use(context => { _flag += "Bar:"; return(Task.CompletedTask); }));
            builder.For <BazException>(PostHandlingAction.ThrowNew, _ => _.Use(context => { _flag += "Baz:"; return(Task.CompletedTask); }));

            builder.Pre(_ => _.Use(context => { _flag += "Foobar:"; return(Task.CompletedTask); }), ex => ex is FoobarException);
            builder.Pre(_ => _.Use(context => { _flag += "Baz:"; return(Task.CompletedTask); }), ex => ex is BazException);
            builder.Post(_ => _.Use(context => { _flag += "Foobar:"; return(Task.CompletedTask); }), ex => ex is FoobarException);
            builder.Post(_ => _.Use(context => { _flag += "Baz:"; return(Task.CompletedTask); }), ex => ex is BazException);


            var policy = builder.Build();
            PostHandlingAction action;

            _flag = "";
            await policy.CreateExceptionHandler(new FoobarException(), out action)(new ExceptionContext(new FoobarException()));

            Assert.Equal("Foobar:Foobar:Foobar:", _flag);


            _flag = "";
            await policy.CreateExceptionHandler(new FooException(), out action)(new ExceptionContext(new FooException()));

            Assert.Equal("Foobar:Foo:Foobar:", _flag);

            _flag = "";
            await policy.CreateExceptionHandler(new BarException(), out action)(new ExceptionContext(new BarException()));

            Assert.Equal("Foobar:Bar:Foobar:", _flag);

            _flag = "";
            await policy.CreateExceptionHandler(new BazException(), out action)(new ExceptionContext(new BazException()));

            Assert.Equal("Baz:Baz:Baz:", _flag);
        }
Beispiel #6
0
        public void For_Must_Be_Exception_Type()
        {
            var builder = new ExceptionPolicyBuilder(new ServiceCollection().BuildServiceProvider());

            Assert.Throws <ArgumentException>(() => builder.For(typeof(int), PostHandlingAction.None, _ => { }));
        }