package com.njcn.web.utils;

import com.njcn.common.pojo.constant.LogInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;

import java.lang.reflect.Method;
import java.util.Objects;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年06月22日 10:25
 */
@Slf4j
public class ControllerUtil {

    /**
     * 针对methodArgumentNotValidException 异常的处理
     * @author cdf
     */
    public static String getMethodArgumentNotValidException(MethodArgumentNotValidException methodArgumentNotValidException) {
        String operate = LogInfo.UNKNOWN_OPERATE;
        Method method = null;
        try {
            method = methodArgumentNotValidException.getParameter().getMethod();
            if (!Objects.isNull(method) && method.isAnnotationPresent(ApiOperation.class)) {
                ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
                operate = apiOperation.value();
            }
        }catch (Exception e){
            log.error("根据方法参数非法异常获取@ApiOperation注解值失败，参数非法异常信息：{}，方法名：{}，异常信息：{}",methodArgumentNotValidException.getMessage(),method,e.getMessage());
        }
        return operate;
    }
}
