How can I pass the exception thorwn by the action in MVCContrib.FluentController CheckValidCall(action)?
[ExportModelStateToTempData]
public ActionResult Index(int itemId, int page)
{
return CheckValidCall(() => MyService.GetResults(itemId, page))
.Valid(x => View(x))
.Invalid(() => RedirectToAction(RestfulAction.Index));
}
When GetResults() throws exception I want to display it in the view. I've tired ModelState
<%if (ViewData.ModelState.ContainsKey("_FORM")) {%>
<div class="notificationError">
<%= ViewData.ModelState["_FORM"].Errors.FirstOrDefault().ErrorMessage %>
</div>
<%}%>
but the ModelState is valid and contains no errors. Is there any way to access the exception message without wrapping service method in try-catch block? If it helps here is my unit test to check ModelState which fails as TestController.ModelState.IsValid is true:
[Fact]
public void ServiceExceptionIsConvertedToModelStateErrorInFluentController()
{
// Set up
MockService.Setup(x => x.GetResults(It.IsAny<int>(), It.IsAny<int>()))
.Throws(new InvalidOperationException("Mocked Service Exception"));
// Excercise
Assert.Throws<InvalidOperationException>(() => TestController.GetResults(1, 1));
// Verify
Assert.False(TestController.ModelState.IsValid);
Assert.True(TestController.ModelState["_FORM"].Errors.Count > 0);
}