package com.zhangmeng.model.bean;

import com.zhangmeng.model.dto.exception.CustomizeException;
import com.zhangmeng.model.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 *
 * @author zhangmeng
 * @version 1.0
 * @date 2021年4月25日10:47:56
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = CustomizeException.class)
    public Result bizExceptionHandler(HttpServletRequest request, CustomizeException e){
        logger.error("发生业务异常！原因是：{}",e.getErrorMsg());
        return Result.error(e.getErrorCode(),e.getErrorMsg());
    }


    /**
     * 处理登录业务异常
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = DisabledException.class)
    public Result bizExceptionHandler(HttpServletRequest request, DisabledException e){
        logger.error("发生业务异常！原因是：{}",e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 处理登录业务异常
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = LockedException.class)
    public Result bizExceptionHandler(HttpServletRequest request, LockedException e){
        logger.error("发生业务异常！原因是：{}",e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 处理空指针的异常
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    public Result exceptionHandler(HttpServletRequest request, NullPointerException e){
        logger.error("发生空指针异常！原因是:",e);
        return Result.error(e.getMessage());
    }
}
