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.BaseCode;
import cn.axzo.framework.domain.web.code.IRespCode;
import org.springframework.stereotype.Component;

import javax.validation.ValidationException;
import java.util.Objects;

/**
 * 修改框架默认的校验信息处理器查找异常类的逻辑
 *
 * @author wangli
 * @since 2023/7/31 11:40
 */
@Component
public class ValidationExceptionResultHandlerAdvice extends AbstractExceptionApiResultHandler<ValidationException> {
    public ValidationExceptionResultHandlerAdvice(RespErrorCodeMappingProperties properties) {
        super(properties);
    }

    @Override
    protected IRespCode decode(ValidationException ex, IRespCode fallbackCode) {
        return BaseCode.BAD_REQUEST;
    }

    @Override
    protected String getMessage(IRespCode respCode, ValidationException ex) {
        return Objects.nonNull(ex.getMessage()) ? ex.getMessage() : respCode.getMessage();
    }

    @Override
    public boolean isRecursive() {
        return true;
    }
}
