All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.server.ErrorHandlingRunnable Maven / Gradle / Ivy

/*
 * Copyright (C) 2000-2024 Vaadin Ltd
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  for the full
 * license.
 */
package com.vaadin.server;

import java.io.Serializable;
import java.util.Objects;

/**
 * Defines the interface to handle exceptions thrown during the execution of a
 * FutureAccess.
 *
 * @since 7.1.8
 * @author Vaadin Ltd
 */
public interface ErrorHandlingRunnable extends Runnable, Serializable {

    /**
     * Handles exceptions thrown during the execution of a FutureAccess.
     * Exceptions thrown by this method are handled by the default error
     * handler.
     *
     * @since 7.1.8
     * @param exception
     *            the thrown exception.
     */
    public void handleError(Exception exception);

    /**
     * Process the given exception in the context of the given runnable. If the
     * runnable extends {@link ErrorHandlingRunnable}, then the exception is
     * passed to {@link #handleError(Exception)} and null is returned. If
     * {@link #handleError(Exception)} throws an exception, that exception is
     * returned. If the runnable does not extend {@link ErrorHandlingRunnable},
     * then the original exception is returned.
     *
     * @since 8.7
     * @param runnable
     *            the runnable for which the exception should be processed, not
     *            null
     * @param exception
     *            the exception to process, not null
     * @return the resulting exception, or null if the exception is
     *         fully processed
     */
    public static Exception processException(Runnable runnable,
            Exception exception) {
        Objects.requireNonNull(runnable, "The runnable cannot be null.");
        if (runnable instanceof ErrorHandlingRunnable) {
            ErrorHandlingRunnable errorHandlingRunnable = (ErrorHandlingRunnable) runnable;

            try {
                errorHandlingRunnable.handleError(exception);
                return null;
            } catch (Exception exceptionFromHandler) {
                return exceptionFromHandler;
            }
        }

        return exception;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy