package cn.axzo.workflow.starter.handler.execute.interceptor;

import cn.axzo.framework.rocketmq.EventConsumer;
import cn.axzo.workflow.starter.common.exception.WorkflowListenerExecutionException;
import cn.axzo.workflow.starter.handler.execute.ListenerExecutor;

import java.util.function.Consumer;

/**
 * 快速失败，出错直接抛出异常，listener不再往下执行
 */
public final class FailFastInterceptor extends AbstractListenerInterceptor {

    @Override
    public <T> void execute(ListenerExecutor executor, Consumer<T> consumer, EventConsumer.Context context, T t) {
        try {
            getNext().execute(executor, consumer, context, t);
        } catch (Throwable e) {
            throw new WorkflowListenerExecutionException(
                    "Failed to invoke the method. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
        }
    }

}
