package com.td.boss.util;

import com.td.boss.common.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

import java.util.List;

/**
 * 捕获异常
 * @author:slambb
 * @date:2021/03/27
 */
@ControllerAdvice
@Slf4j
public class MyExceptionHandler {

//    //拦截登录异常
//    @ExceptionHandler(value = AuthorizeException.class)
//    @ResponseBody
//    public ResultVO handlerAuthorizeException(AuthorizeException e){
//        log.info("捕获信息异常{}，{}",e.getCode(),e.getMessage());
//        return ResultVOUtil.error(e.getCode(),e.getMessage());
//    }
//
//    //用户信息异常
//    @ExceptionHandler(value = UserException.class)
//    @ResponseBody
//    public ResultVO handlerUserException(UserException e){
//
//        return ResultVOUtil.error(e.getCode(),e.getMessage());
//    }
//
//    //表单错误
//    @ExceptionHandler(value = BindException.class)
//    @ResponseBody
//    public ResultVO handlerBindException(BindException e){
////        log.info(e.getMessage());
////        log.info(e.getAllErrors().toString());
//        List<ObjectError> errors=e.getAllErrors();
//        String msg=errors.get(0).getDefaultMessage();//获取自错误信息
//        return ResultVOUtil.error(200 ,msg);
//    }


    //捕获设置了形参,但是在访问该方法时,没有传参的异常
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    @ResponseBody
    public Result<String> handlerServletRequestParameterException(MissingServletRequestParameterException e){
        return Result.of(null,false,e.getMessage());


    }

    //捕获设置了形参,但是在访问该方法时,没有传参的异常
    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public Result<String> handlerServletRequestParameterException(IllegalArgumentException e){
        return Result.of(null,false,e.getMessage());


    }

}
