package cn.axzo.workflow.server.advice;

import cn.axzo.framework.autoconfigure.web.exception.RespErrorCodeMappingProperties;
import cn.axzo.framework.autoconfigure.web.exception.handler.AbstractExceptionApiResultHandler;
import cn.axzo.framework.domain.web.code.IRespCode;
import cn.axzo.framework.domain.web.code.RespCode;
import cn.axzo.workflow.core.common.exception.WorkflowEngineException;
import cn.axzo.workflow.server.common.config.property.WorkflowProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

import static org.springframework.http.HttpStatus.OK;

/**
 * 基于 Flowable 框架在业务功能开发过程中,抛出的异常处理器
 *
 * @author wangli
 * @since 2023/12/8 12:52
 */
@Component
public class WorkflowExceptionResultHandlerAdvice extends AbstractExceptionApiResultHandler<WorkflowEngineException> {

    @Resource
    private WorkflowProperties workflowProperties;

    public WorkflowExceptionResultHandlerAdvice(RespErrorCodeMappingProperties properties) {
        super(properties);
    }

    @Override
    protected IRespCode decode(WorkflowEngineException ex, IRespCode fallbackCode) {
        List<String> filterExceptionCodes = workflowProperties.getFilterExceptionCode();
        if (filterExceptionCodes.contains(ex.getCode())) {
            return new RespCode(String.valueOf(OK.value()), ex.getMessage());
        }
        return new RespCode(ex.getCode(), ex.getMessage());
    }
}
