Internal Error view

Hi,

somebody know if is possible to change the default Internal Error view (look the attachment image)?

How I can do it?
Thanks
17430516.png

Hi Gianluca,
I think VaadinService.setSystemMessagesProvider may be what you are looking for.

Take a look at following link; it refers to Vaadin 8 but it should work also for Flow.
https://vaadin.com/docs/v8/framework/application/application-errors.html

A VaadinServiceInitListener may be a good hook to inject the custom message provider.

HTH
Marco

I wrote two error handlers in my vaadin 12 application. The first one is called when an error occurs during a navigation, such as an exception being thrown on the onAttach event. The second error handler is called when an exception occurs in a lambda callback.

/**
 * Add our own default error handler for uncaught exceptions thrown in lambda
 * expressions. Vaadin's other error handlers are not called in this case.
 * <p>
 * From vaadin's forum: Normally, the error handler closest to the problem is
 * used, so if there is one defined for the UI, it is used rather than the
 * session error handler. The session error handler then takes care of
 * exceptions that occur outside the contexts where a request to a UI is being
 * processed (session initialization, before and after the phases where
 * UI/components handle a request, ...).
 * </p>
 */
@SpringComponent
public class ApplicationServlet implements VaadinServiceInitListener, SessionInitListener {
	
	@Override
	public void serviceInit(ServiceInitEvent event) {
		event.getSource().addSessionInitListener(this);
	}

	@Override
	public void sessionInit(SessionInitEvent event) throws ServiceException {
		event.getSession().setErrorHandler(new SessionErrorHandler());
	}
}

/**
 * Custom error handler used when exceptions are thrown in lambdas, or are not caught by
 * a HasErrorParameter handler.
 */
public class SessionErrorHandler implements ErrorHandler {

	@Override
    public void error(ErrorEvent event) {
        Throwable t = event.getThrowable();
        ... handle exception ...
    }
}

/**
 * Error page displayed when exception occurs during route navigation, including in an onAttach() method.
 */
@HtmlImport("frontend://src/error/routeexception.html")
@Tag("route-error")
@ParentLayout(CoverImageLayout.class)
@Log4j2
public class RouteExceptionErrorHandler extends InternalServerError {

	private final String supportEmail;

	public RouteExceptionErrorHandler(@Value("${web.support-email}") String supportEmail)
	{
		this.supportEmail = supportEmail;
	}

	@Override
	public int setErrorParameter(BeforeEnterEvent event, ErrorParameter<Exception> parameter) {

        String path = event.getLocation().getPath();
        String additionalInfo;
        Exception e = parameter.getCaughtException();
        if (parameter.hasCustomMessage()) {
            additionalInfo = parameter.getCustomMessage();
        }
        else if(e.getMessage() != null) {
        	additionalInfo = e.getMessage();
        }
        else {
        	additionalInfo = e.toString();
        }
        
        log.error("Unhandled exception routing to {} [{}]
", path, additionalInfo, e);

        Div container = new Div();
        HtmlComponent title1 = new H4("Unexpected error occurred navigating to "+path);
        HtmlComponent title2 = new Paragraph(additionalInfo);
        
        Span text1 = new Span("Please try the request again. If the error persists, contact support at ");
        Anchor link = new Anchor("mailto:"+supportEmail, supportEmail);
        Span text2 = new Span(".");
        Paragraph p1 = new Paragraph(text1, link, text2);
        
        RouterLink homelink = Component.from(
        		ElementFactory.createRouterLink("", "Go to the front page."),
        		RouterLink.class);
        
        container.add(title1, title2, p1, new Paragraph(homelink));

        this.getElement().removeAllChildren();
        this.getElement().appendChild(container.getElement());
        
        return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
	}
}