package cn.axzo.workflow.core.engine.cmd;

import cn.axzo.workflow.core.common.exception.WorkflowEngineException;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.FlowableOptimisticLockingException;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO
 *
 * @author wangli
 * @since 2024/5/21 09:46
 */
public class CustomCommandContext extends CommandContext {
    private static final Logger LOGGER = LoggerFactory.getLogger(CommandContext.class);

    public CustomCommandContext(Command<?> command) {
        super(command);
    }

    @Override
    protected void logException() {
        if (exception instanceof FlowableException && !((FlowableException) exception).isLogged()) {
            return;
        }

        if (exception instanceof FlowableOptimisticLockingException) {
            // reduce log level, as normally we're not interested in logging this exception
            LOGGER.debug("Optimistic locking exception : {}", exception.getMessage(), exception);

        } else if (exception instanceof FlowableException && ((FlowableException) exception).isReduceLogLevel()) {
            // reduce log level, because this may have been caused because of job deletion due to cancelActiviti="true"
            LOGGER.info("Error while closing command context", exception);
        } else if (exception instanceof WorkflowEngineException) {
            LOGGER.warn("Workflow error while closing command context", exception);
        } else {
            LOGGER.error("Error while closing command context", exception);

        }
    }
}
