如何在 MVCContrib 中显示来自流畅控制器的无效调用异常?

发布于 2024-08-31 22:15:12 字数 1447 浏览 5 评论 0原文

如何传递 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ杯具 2024-09-07 22:15:12

我设法通过重写 MvcContrib.FluentController.AbsteactFluentController.ExecuteCheckValidCall(Func action): 将异常传递到 ModelState 中:

    protected override object ExecuteCheckValidCall(Func<object> action)
    {
        try
        {
            return base.ExecuteCheckValidCall(action);
        }
        catch (Exception exception)
        {
            ModelState.AddModelError("_Exception", exception);
            return null;
        }
    }

由 CheckValidCall 调用。然而,该方法被描述为“仅用于测试目的,不应使用”,替代方法是重写 MvcContrib.FluentController.AbstractFluentController.CheckValidCall()。

I've manage to pass exception into ModelState by overriding MvcContrib.FluentController.AbsteactFluentController.ExecuteCheckValidCall(Func action):

    protected override object ExecuteCheckValidCall(Func<object> action)
    {
        try
        {
            return base.ExecuteCheckValidCall(action);
        }
        catch (Exception exception)
        {
            ModelState.AddModelError("_Exception", exception);
            return null;
        }
    }

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文