wanghc
2023-07-04 a042e0e63f86a0880d20e57c14ec4a60b65801fb
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
package com.jttech.pfcs.conf;
 
import com.jttech.pfcs.vo.resp.ResponseVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
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;
 
/**
 * 全域异常处理
 * @author wanghc
 * @version 1.0.0
 * @date 2023-03-09
 */
@ControllerAdvice
public class GlobalExceptionHandler {
 
    private Logger mLogger = LoggerFactory.getLogger(getClass());
 
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseVo exceptionHandler(HttpServletRequest request, Exception exception) {
        mLogger.error("请求发生异常 error={}", exception);
        String errMsg = StringUtils.isEmpty(exception.getMessage()) ? "服务异常" : exception.getMessage();
        //截取前面20个字符
        errMsg = errMsg.substring(0,Math.min(20, errMsg.length()));
        return ResponseVo.fail(-999, errMsg);
    }
}