GlobalExceptionHandlerAdvice.java 1.01 KB
package com.cnlive.shenhe.config;

import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: yan-zhao-wang
 * @DateTime: 2022-09-20 9:08
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandlerAdvice {
    /**
     * 自定义运行时异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MyException.class)
    public ResponseBean handleMyException(MyException e) {
        log.info("运行时异常为:{}", e.getMessage());
        return new ResponseBean(666, e.getMessage(), null);
    }

    /**
     * 其它全局异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseBean handleException(Exception e) {
        log.info("其它异常为:{}", e.getMessage());
        return new ResponseBean(ErrorEnum.ERROR, null);
    }
}