package net.educoder.ecsonar.config;

import net.educoder.ecsonar.exception.BusinessException;
import net.educoder.ecsonar.utils.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
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.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: youys
 * @Date: 2022/1/18
 * @Description: 统一异常处理
 */
@ControllerAdvice
public class CustomExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomExceptionHandler.class);

    /**
     * 处理Exception异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult handleException(HttpServletRequest request, Exception e) {
        LOGGER.error("接口:{},Exception异常:" + e.getMessage(), request.getRequestURI(), e);
        return ResponseResult.error("系统出问题了");
    }

    /**
     * 处理BindException异常
     *
     * @param request
     * @param be
     * @return
     */
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public ResponseResult handleException(HttpServletRequest request, BindException be) {
        LOGGER.error("接口:{},BindException异常:" + be.getMessage(), request.getRequestURI());
        return ResponseResult.error("参数错误");
    }

    /**
     * 处理BusinessException异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public ResponseResult handleException(HttpServletRequest request, BusinessException e) {
        LOGGER.error("接口:" + request.getRequestURI() + ",BusinessException异常:", e);
        return ResponseResult.error(e.getMessage());
    }

    /**
     * 处理 MaxUploadSizeExceededException 异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public ResponseResult handleException(HttpServletRequest request, MaxUploadSizeExceededException e) {
        LOGGER.error("接口:{},文件上传超过最大设置的值:" + e.getMessage(), request.getRequestURI());
        return ResponseResult.error("文件上传超过最大设置的值");
    }

    /**
     * 405，请求方式错误
     *
     * @param ex
     * @param request
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ResponseResult handleHttpRequestMethodNotSupported(HttpServletRequest request, HttpRequestMethodNotSupportedException ex) {
        LOGGER.error("接口:{},请求方式错误405:" + ex.getMessage(), request.getRequestURI(), ex);
        return ResponseResult.error("请求方式错误405");
    }

    /**
     * 缺少必填参数错误
     *
     * @param ex
     * @param request
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public ResponseResult handleMissingServletRequestParameter(HttpServletRequest request, MissingServletRequestParameterException ex) {
        LOGGER.error("接口:{},缺少必传参数400:" + ex.getMessage(), request.getRequestURI(), ex);
        return ResponseResult.error("缺少必填参数");
    }
}
