shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.walker.web;
 
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * 定义在WEB层异常统一处理的机制。
 * @author 时克英
 * @date 2022-10-12
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
 
    protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private static final String ERROR_MESSAGE = "系统内部异常";
 
    /**
     * 定义统一处理业务在WEB中抛出的运行异常。
     * <pre>
     *     1) 在 Controller 层中,通常不会抛出运行异常,只有特定情况下,这是一种处理方式。
     *     2) 在 Controller 层中,开发人员想直接使用 Transaction 数据库事务,此时需要抛出异常中断事务。
     * </pre>
     * @param request
     * @param e
     * @param response
     * @return
     */
    @ExceptionHandler(WebRuntimeException.class)
    public ResponseValue<String> webRuntimeExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response){
        response.setCharacterEncoding(StringUtils.DEFAULT_CHARSET_UTF8);
        response.setContentType(Constants.APPLICATION_JSON);
        response.setStatus(Constants.HTTP_SUCCESS);
 
        WebRuntimeException webRuntimeException = (WebRuntimeException)e;
 
        String error = webRuntimeException.getMsg();
        if(StringUtils.isEmpty(error)){
            error = ERROR_MESSAGE;
        }
        logger.error("业务抛出web异常:" + webRuntimeException.getMessage(), e);
//        return ResponseValue.error(webRuntimeException.getCode(), error);
        return ResponseValue.exception();
    }
 
    @ExceptionHandler(RuntimeException.class)
    public ResponseValue<String> runtimeExceptionHandler(HttpServletRequest request, final Exception e){
//        response.setCharacterEncoding(StringUtils.DEFAULT_CHARSET_UTF8);
//        response.setContentType(Constants.APPLICATION_JSON);
//        response.setStatus(Constants.HTTP_SUCCESS);
//        WebRuntimeException webRuntimeException = (WebRuntimeException)e;
//        String error = e.getMsg();
//        if(StringUtils.isEmpty(error)){
//            error = ERROR_MESSAGE;
//        }
        logger.error("请求地址'{}',发生运行异常.", request.getRequestURI(), e);
//        return ResponseValue.error(e.getMessage());
        return ResponseValue.exception();
    }
 
    @ExceptionHandler(Exception.class)
    public ResponseValue<String> exceptionHandler(HttpServletRequest request, final Exception e){
        logger.error("请求地址'{}',发生系统异常.", request.getRequestURI(), e);
//        return ResponseValue.error(e.getMessage());
        return ResponseValue.exception();
    }
}