wanghc
2023-06-27 933ee70c42092a600696428cefd178de805d0439
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
package com.jttech.pfcs.conf;
 
import com.jttech.pfcs.vo.resp.ResponseVo;
import org.slf4j.Logger;
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 {
 
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseVo exceptionHandler(HttpServletRequest request, Exception exception) {
        String errMsg = StringUtils.isEmpty(exception.getMessage()) ? "服务异常" : exception.getMessage();
        //截取前面20个字符
        errMsg = errMsg.substring(0,Math.min(20, errMsg.length()));
        return ResponseVo.fail(-999, errMsg);
    }
}