如何在 MVCContrib 中显示来自流畅控制器的无效调用异常?
如何传递 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));
}
当 GetResults() 抛出异常时,我想将其显示在视图中。我已经厌倦了 ModelState
<%if (ViewData.ModelState.ContainsKey("_FORM")) {%>
<div class="notificationError">
<%= ViewData.ModelState["_FORM"].Errors.FirstOrDefault().ErrorMessage %>
</div>
<%}%>
,但 ModelState 是有效的并且不包含任何错误。有没有办法在不将服务方法包装在 try-catch 块中的情况下访问异常消息?如果这里有帮助的话,我的单元测试会检查 ModelState ,但由于 TestController.ModelState.IsValid 为 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法通过重写 MvcContrib.FluentController.AbsteactFluentController.ExecuteCheckValidCall(Func action): 将异常传递到 ModelState 中:
由 CheckValidCall 调用。然而,该方法被描述为“仅用于测试目的,不应使用”,替代方法是重写 MvcContrib.FluentController.AbstractFluentController.CheckValidCall()。
I've manage to pass exception into ModelState by overriding MvcContrib.FluentController.AbsteactFluentController.ExecuteCheckValidCall(Func action):
Which is called by CheckValidCall. However the method is described as "public for testing purposes only and shouldn't be used" the alternative way of doing it is to override MvcContrib.FluentController.AbstractFluentController.CheckValidCall().