/// <inheritdoc />
        public override void Handle(ExceptionHandlerContext context)
        {
            if (context?.Exception == null)
            {
                base.Handle(context);
                return;
            }

            object apiEvent = null;

            if (context.Exception is AutoResponseException autoResponseException)
            {
                apiEvent = autoResponseException.EventObject;
            }

            if (apiEvent == null)
            {
                apiEvent = context.Exception?.GetType().GetProperty(this.domainResultPropertyName)
                           ?.GetValue(context.Exception, null);
            }

            if (apiEvent == null)
            {
                apiEvent = new ServiceErrorApiEvent(context.Exception);
            }

            context.Result = new AutoResponseResult(context.Request, apiEvent);
            base.Handle(context);
        }
示例#2
0
        public void AddMappingGetResponseReturnsExpectedResponse(
            ServiceErrorApiEvent apiEvent,
            ServiceErrorHttpResponse httpResponse)
        {
            var addMappings = new Action <ExceptionHttpResponseConfiguration>(
                configuration =>
            {
                configuration.AddMapping <ServiceErrorApiEvent>(
                    (e, c) => httpResponse);
            });

            var mapper   = new TestApiEventHttpResponseMapper(addMappings);
            var response = mapper.GetHttpResponse(null, apiEvent);

            Assert.Equal(httpResponse, response);
        }
示例#3
0
        public void RemoveMappingThatDoesNotExistThrowsInvalidOperationException(
            ServiceErrorApiEvent apiEvent)
        {
            var addMappings = new Action <ExceptionHttpResponseConfiguration>(
                configuration =>
            {
                configuration.RemoveMapping <ServiceErrorApiEvent>();
            });

            Assert.Throws <InvalidOperationException>(
                () =>
            {
                var mapper = new TestApiEventHttpResponseMapper(addMappings);
                mapper.GetHttpResponse(null, apiEvent);
            });
        }
示例#4
0
        public void RemoveMappingRemovesTheMapping(
            ServiceErrorApiEvent apiEvent,
            ServiceErrorHttpResponse httpResponse)
        {
            var addMappings = new Action <ExceptionHttpResponseConfiguration>(
                configuration =>
            {
                configuration.AddMapping <ServiceErrorApiEvent>(
                    (e, c) => httpResponse);

                configuration.RemoveMapping <ServiceErrorApiEvent>();
            });

            var mapper   = new TestApiEventHttpResponseMapper(addMappings);
            var response = mapper.GetHttpResponse(null, apiEvent);

            Assert.Null(response);
        }
示例#5
0
        public void UpdateMappingUpdatesTheResponse(
            ServiceErrorApiEvent apiEvent,
            ServiceErrorHttpResponse httpResponse,
            ServiceErrorHttpResponse httpResponseReplacement)
        {
            var addMappings = new Action <ExceptionHttpResponseConfiguration>(
                configuration =>
            {
                configuration.AddMapping <ServiceErrorApiEvent>(
                    (e, c) => httpResponse);

                configuration.UpdateMapping <ServiceErrorApiEvent>(
                    (e, c) => httpResponseReplacement);
            });

            var mapper   = new TestApiEventHttpResponseMapper(addMappings);
            var response = mapper.GetHttpResponse(null, apiEvent);

            Assert.Equal(httpResponseReplacement, response);
        }
示例#6
0
        public void DuplicateApiEventThrowsInvalidOperationException(
            ServiceErrorApiEvent apiEvent,
            ServiceErrorHttpResponse httpResponse)
        {
            var addMappings = new Action <ExceptionHttpResponseConfiguration>(
                configuration =>
            {
                configuration.AddMapping <ServiceErrorApiEvent>(
                    (e, c) => httpResponse);

                configuration.AddMapping <ServiceErrorApiEvent>(
                    (e, c) => httpResponse);
            });

            Assert.Throws <InvalidOperationException>(
                () =>
            {
                var mapper = new TestApiEventHttpResponseMapper(addMappings);
                mapper.GetHttpResponse(null, apiEvent);
            });
        }