GlobalExceptionHandlerAdvice.java
1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
}
}