kongzy
2023-09-22 3124f3a5b7f45d043b228829b6b3a2e541b31574
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.nms.swspkmas_standalone.exception;
 
import cn.hutool.core.util.StrUtil;
import com.nms.swspkmas_standalone.response.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
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;
import java.sql.SQLSyntaxErrorException;
 
/**
 * 全局异常处理类
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
 
    @Autowired
    private HttpServletRequest request;
 
    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public CommonResult handle(ApiException e) {
        writeExceptionLogFile(e);
        if (e.getErrorCode() != null) {
            return CommonResult.failed(e.getErrorCode());
        }
        return CommonResult.failed(e.getMessage());
    }
 
    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public CommonResult handleValidException(MethodArgumentNotValidException e) {
        writeExceptionLogFile(e);
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getField()+fieldError.getDefaultMessage();
            }
        }
        return CommonResult.validateFailed(message);
    }
 
    @ResponseBody
    @ExceptionHandler(value = BindException.class)
    public CommonResult handleValidException(BindException e) {
        writeExceptionLogFile(e);
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getField()+fieldError.getDefaultMessage();
            }
        }
        return CommonResult.validateFailed(message);
    }
 
 
    /**
     * 500 其他服务器异常
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public CommonResult otherExceptionHandler(Exception e) {
        writeExceptionLogFile(e);
        String message = StrUtil.isNotBlank(e.getMessage()) ? e.getMessage() : e.toString();
        return CommonResult.failed(message);
    }
 
 
    @ResponseBody
    @ExceptionHandler(value = SQLSyntaxErrorException.class)
    public CommonResult handleSQLSyntaxErrorException(SQLSyntaxErrorException e) {
        writeExceptionLogFile(e);
        String message = e.getMessage();
        if (StrUtil.isNotEmpty(message) && message.contains("denied")) {
            message = "暂无修改权限,如需修改数据可本地搭建后台服务!";
        }
        return CommonResult.failed(message);
    }
 
    private void writeExceptionLogFile(Exception e) {
        String url=request.getRequestURL().toString();
        String message = StrUtil.isNotBlank(e.getMessage()) ? e.getMessage() : e.toString();
        log.error("error={}", e);
        log.error(message+",url={}",url);
    }
}