package com.joju.datamanager.common.exception;

import com.joju.datamanager.common.result.Result;
import com.joju.datamanager.common.result.ResultEnum;
import com.joju.datamanager.common.result.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： Mr.zs
 * @date： 2023/12/27
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {


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

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

    /**
     * 处理空指针的异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest req, NullPointerException e) {
        logger.error("发生空指针异常！原因是:", e);
//        return ResultResponse.error(ExceptionEnum.BODY_NOT_MATCH);
        return ResultUtil.failure(ResultEnum.NULL_POINT);
    }

    /**
     * 处理其他异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest req, Exception e) {
        logger.error("未知异常！原因是:", e);
        return ResultUtil.failure(ResultEnum.SERVICE_FAILURE);
    }
}
