package com.jiayue.ssi.config;

import com.jiayue.ssi.constant.CustomException;
import com.jiayue.ssi.util.IPUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
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.bind.annotation.ResponseStatus;
import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 *
 * @author xsl
 * @since 2023/04/28
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionAdvice {
    @ExceptionHandler(value = CustomException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public void handleException(HttpServletRequest request, Exception e) throws Exception {
        String ipAddr = IPUtils.getIpAddr(request);
        log.error("IP:"+ipAddr+"访问异常：", e);
    }
}
