/// <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); }
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); }
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); }); }
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); }
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); }
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); }); }