How to handle or change Invalid JSON from Server Vaadin 21

346 Views Asked by At

I am trying to display a custom error when there is a session expired or a internal server error using VaadinService.getCurrent().setSystemMessagesProvider(PARAM);

Right know, Vaadin won't display the right error when I am disconnected due to connexion from another browser, and displays me this message : "Invalid JSON From server", followed by a HTML body, which is not user friendly.

I am using Vaadin 21, and this is what I do now :

import org.springframework.stereotype.Component;

import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.server.CustomizedSystemMessages;
import com.vaadin.flow.server.ServiceException;
import com.vaadin.flow.server.ServiceInitEvent;
import com.vaadin.flow.server.SessionInitEvent;
import com.vaadin.flow.server.SessionInitListener;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinServiceInitListener;

import my.project.utils.security.SecurityUtils;
import my.project.vaadin.ConstantesRoutes;
import my.project.vaadin.view.login.ChangePasswordView;
import my.project.vaadin.view.login.ForgotPasswordView;
import my.project.vaadin.view.login.LoginView;

/**
 * Allow the navigation listener to all the route
 * This provide the re-reroute action if the user isn't login or not allow to access
 *
 */
@Component
public class ConfigureUIServiceInitListener implements VaadinServiceInitListener, SessionInitListener {


    /**
     * Init the service to provide the beforeEnter action
     *
     * @param event at the serviceInit
     */
    @Override
    public void serviceInit(final ServiceInitEvent event) {
        ...
        event.getSource().addSessionInitListener(this);
    }

    /**
     * Reroutes the user if (s)he is not authorized to access the view or not login
     *
     * @param event
     *            before navigation event with event details
     */
    private void beforeEnter(final BeforeEnterEvent event) {
        ...
    }

    @Override
    public void sessionInit(final SessionInitEvent event) throws ServiceException {
        VaadinService.getCurrent().setSystemMessagesProvider(systemMessagesInfo -> {
            final CustomizedSystemMessages messages = new CustomizedSystemMessages();
            messages.setInternalErrorCaption("Internal Server Error");
            messages.setInternalErrorMessage("This is bad.");
            messages.setInternalErrorNotificationEnabled(true);
            messages.setInternalErrorURL("http://vaadin.com/");

            messages.setSessionExpiredCaption("Comm Err session");
            messages.setSessionExpiredMessage("This is bad. session");
            messages.setSessionExpiredNotificationEnabled(true);
            messages.setSessionExpiredURL(ConstantesRoutes.LOGIN_URL);
            return messages;
        });

    }
}

Any idea ? Thank you !

0

There are 0 best solutions below