2

当 DataProvider 的 fetch 或 count 方法抛出异常时,例如因为用户未被授权,我如何集中处理这些异常?我知道HasErrorParameter当路由时抛出异常时,有界面可以显示错误视图。但是当 DataProvider 抛出异常时,并不会触发这些错误视图。

例子:

  new AbstractBackEndDataProvider<String, Void>() {
        @Override
        protected Stream<String> fetchFromBackEnd(Query<String, Void> query) {
            ...
        }

        @Override
        protected int sizeInBackEnd(Query<String, Void> query) {
            throw new UnsupportedOperationException("test");
        }
    }

@Route("failed")
public class FailView extends VerticalLayout 
         implements HasErrorParameter<UnsupportedOperationException> {...}

即使我try catch在 DataProvider 方法中执行操作,我也看不到如何仅通过使用捕获的异常而不是视图组件类来导航到适当的错误视图(这不会触发setErrorParameter方法)。

顺便说一句:我错过了 Vaadin Flow 13 文档中的路由器异常处理主题。我想知道他们为什么删除它。

4

1 回答 1

3

我相信路由时不会发生的所有异常都会被提供给发生错误的 VaadinSession 的 ErrorHandler。

设置 ErrorHandler 的最佳方法似乎是sessionInit在自定义中覆盖该方法SessionInitListener

您可以在自定义 VaadinServletSessionInitListener的方法中添加自定义。servletInitialized

class CustomServlet extends VaadinServlet{
    @Override
    protected void servletInitialized() throws ServletException {
        super.servletInitialized();
        getService().addSessionInitListener(new CustomSessionInitListener());
    }
}

并且SessionInitListener(在本例中CustomSessionInitListener)必须设置初始化会话的 errorHandler。

class CustomSessionInitListener implements SessionInitListener{
    @Override
    public void sessionInit(SessionInitEvent event) throws ServiceException {
        event.getSession().setErrorHandler(new CustomErrorHandler());
    }
}

有关如何创建自己的 Servlet 的更多信息,请查看Vaadin 的教程页面(您需要向下滚动到“自定义 Vaadin Servlet”)

编辑:要显示错误页面,您需要让 Vaadin 重新路由到错误。为了实现这一点,我们可以使用BeforeEnterEvent,BeforeEnterEvents有一个rerouteToError方法可以让 Vaadin 显示我们的 ErrorView。

但是我们也想传递 Exception 实例,所以我们也必须存储它。我在下面的课程中做到了这一点:

@Route("error-view") // Route shown in the user's browser
public class ErrorViewShower extends Div implements BeforeEnterObserver {
    // Class to store the current Exception of each UI in
    private static class UIExceptionContainer extends HashMap<UI, Exception> {

    }

    // Method to call when we want to show an error
    public static void showError(Exception exception) {
        UIExceptionContainer exceptionContainer = VaadinSession.getCurrent().getAttribute(UIExceptionContainer.class);
        // Creating and setting the exceptionContainer in case it hasn't been set yet.
        if (exceptionContainer == null) {
            exceptionContainer = new UIExceptionContainer();
            VaadinSession.getCurrent().setAttribute(UIExceptionContainer.class, exceptionContainer);
        }

        // Storing the exception for the beforeEnter method
        exceptionContainer.put(UI.getCurrent(), exception);

        // Now we navigate to an Instance of this class, to use the BeforeEnterEvent to reroute to the actual error view
        UI.getCurrent().navigate(ErrorViewShower.class);// If this call doesn't work you might want to wrap into UI.access
    }

    @Override
    public void beforeEnter(BeforeEnterEvent event) {
        UIExceptionContainer exceptionContainer = VaadinSession.getCurrent().getAttribute(UIExceptionContainer.class);

        // Retrieving the previously stored exception. You might want to handle if this has been called without setting any Exception.
        Exception exception = exceptionContainer.get(UI.getCurrent());

        //Clearing out the now handled Exception
        exceptionContainer.remove(UI.getCurrent());

        // Using the BeforeEnterEvent to show the error
        event.rerouteToError(exception, "Possible custom message for the ErrorHandler here");
    }

}

它与错误处理程序结合使用如下所示:

public class CustomErrorHandler implements ErrorHandler {
    @Override
    public void error(ErrorEvent event) {
        // This can easily throw an exception itself, you need to add additional checking before casting.
        // And it's possible that this method is called outside the context of an UI(when a dynamic resource throws an exception for example)
        Exception exception = (Exception) event.getThrowable();
        ErrorViewShower.showError(exception);
    }

}

Edit2:事实证明,内部方法调用内部发生的异常不会由 UI 的 ErrorHandler 或 VaadinSession 的 ErrorHandler 处理,而是由另一个错误处理程序处理,这会导致客户端终止并显示错误通知,

一种解决方案是在 DataProvider 的方法中捕获异常并将它们传递给ErrorViewShower.showError()并仍然返回,而没有任何异常向上飞堆栈跟踪。(或者不要自己抛出任何异常,而只需将 new 传递给ErrorViewShower.showError()方法)。

通过正常返回,Vaadin 甚至都不知道出了什么问题。
ErrorViewShower.showError()调用ui.navigate,该导航命令似乎在对 DataProvider 的调用之后“排队”,这意味着用户的视图将在同一个请求中发生变化。

具有这种实现的数据提供者:

new AbstractBackEndDataProvider<String, Void>() {
    @Override
    protected Stream<String> fetchFromBackEnd(Query<String, Void> query) {
        try{
            //Code that can throw an Exception here
        }catch(Exception e){
            ErrorViewShower.showError(e);
            //We have to make sure that query.getLimit and query.getOffset gets called, otherwise Vaadin throws an Exception with the message "the data provider hasn't ever called getLimit() method on the provided query. It means that the the data provider breaks the contract and the returned stream contains unxpected data."
            query.getLimit();
            query.getOffset();
            return Stream.of(); //Stream of empty Array to return without error
        }
    }

    @Override
    protected int sizeInBackEnd(Query<String, Void> query) {
        //Second way i mentioned, but this will not catch any Exception you didn't create, where as the try...catch has no way to let any Exception reach Vaadin.
        if(badThingsHappened){
            ErrorViewShower.showError(new UnsupportedOperationException("Bad things..."));
            return 0;//Exiting without error
        }
    }
}
于 2019-03-21T15:26:17.510 回答