public WhenHandlingDomainExceptionUsingCustomHandling() { _context = new TestHttpContext(); var myDomainException = new MyDomainException(); _customApiProblem = new ExtentedApiProblem { ProblemInstanceUri = "123piano:id", Title = "custom title", Detail = "expected detail text", HttpStatus = StatusCodes.Status412PreconditionFailed, ProblemTypeUri = "complex/problem", ExtraProperty = "Extra, extra, read al of it in this extra property." }; _secondCustomApiProblem = new ExtentedApiProblem { ProblemInstanceUri = "nonono:001", Title = "Empty", Detail = "details", HttpStatus = StatusCodes.Status205ResetContent, ProblemTypeUri = "type/unknown", ExtraProperty = "empty as well", }; var customDomainExceptionHandler = new Mock <IExceptionHandler>(); customDomainExceptionHandler .Setup(handler => handler.Handles(It.IsAny <DomainException>())) .Returns(true); customDomainExceptionHandler .Setup(handler => handler.GetApiProblemFor(It.IsAny <DomainException>())) .ReturnsAsync(() => _customApiProblem); customDomainExceptionHandler .SetupGet(handler => handler.HandledExceptionType) .Returns(typeof(MyDomainException)); var extraCustomDomainExceptionHandler = new Mock <IExceptionHandler>(); extraCustomDomainExceptionHandler .Setup(handler => handler.Handles(myDomainException)) .Returns(true); extraCustomDomainExceptionHandler .Setup(handler => handler.GetApiProblemFor(myDomainException)) .ReturnsAsync(() => _secondCustomApiProblem); var exceptionHandler = new ExceptionHandler( Mock.Of <ILogger <ApiExceptionHandler> >(), new[] { customDomainExceptionHandler.Object, extraCustomDomainExceptionHandler.Object }); exceptionHandler.HandleException(myDomainException, _context).GetAwaiter().GetResult(); }
public async Task HandlesDomainException() { var exception = new MyDomainException(); TestProblemDetailsException( async() => await _exceptionHandler.HandleException(exception, _context), basicApiProblem => { basicApiProblem.Title.Should().Be("Er heeft zich een fout voorgedaan!"); basicApiProblem.Detail.Should().Be("Exception of type 'Be.Vlaanderen.Basisregisters.Api.Tests.MyDomainException' was thrown."); basicApiProblem.HttpStatus.Should().Be(StatusCodes.Status400BadRequest); basicApiProblem.ProblemInstanceUri.Should().NotBeNullOrWhiteSpace(); basicApiProblem.ProblemTypeUri.Should().Be("urn:be.vlaanderen.basisregisters.api:domain"); }); }