18045010223
2025-07-07 0d3a683a0c97154b1f2e6657398664537e4e3e82
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
99
100
101
package org.yzh.commons.spring;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import org.yzh.commons.model.APICodes;
import org.yzh.commons.model.APIException;
import org.yzh.commons.model.R;
 
import java.util.List;
 
@Slf4j
@RestControllerAdvice
public class ExceptionController {
 
    @ExceptionHandler(AsyncRequestNotUsableException.class)
    public void onAsyncRequestNotUsableException(AsyncRequestNotUsableException e) {
        log.warn("异步请求已断开:{}", e.getMessage());
    }
 
    @ExceptionHandler(IllegalArgumentException.class)
    public R<?> onIllegalArgumentException(IllegalArgumentException e) {
        log.warn("系统异常:", e);
        return R.error(APICodes.InvalidParameter, e.getMessage());
    }
 
    @ExceptionHandler(APIException.class)
    public APIException onAPIException(APIException e) {
        return e;
    }
 
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public R<?> onHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.warn("系统异常:", e);
        return R.error(APICodes.TypeMismatch, e);
    }
 
    @ExceptionHandler(BindException.class)
    public R<?> onBindException(BindException e) {
        List<FieldError> fieldErrors = e.getFieldErrors();
        StringBuilder sb = new StringBuilder();
        for (FieldError fieldError : fieldErrors)
            sb.append(fieldError.getField()).append(fieldError.getDefaultMessage());
        return R.error(APICodes.MissingParameter, sb.toString());
    }
 
    @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
    public R<?> onHttpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException e) {
        return R.error(APICodes.NotAcceptable);
    }
 
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public R<?> onHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        return R.error(APICodes.UnsupportedMediaType);
    }
 
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R<?> onHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return R.error(APICodes.NotImplemented);
    }
 
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<?> onMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        FieldError fieldError = e.getBindingResult().getFieldError();
        if (fieldError != null)
            return R.error(fieldError.getDefaultMessage());
        return R.error(APICodes.TypeMismatch, e.getBindingResult().toString());
    }
 
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public R<?> onMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        return R.error(APICodes.MissingParameter, ":" + e.getParameterName());
    }
 
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public R<?> onMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
        return R.error(APICodes.TypeMismatch, ":" + e.getName() + "=" + e.getValue(), e);
    }
 
    @ExceptionHandler(NoHandlerFoundException.class)
    public R<?> onNoHandlerFoundException(NoHandlerFoundException e) {
        return R.error(APICodes.NotFound);
    }
 
    @ExceptionHandler(NoResourceFoundException.class)
    public R<?> onNoResourceFoundException(NoResourceFoundException e) {
        return R.error(APICodes.NotFound);
    }
}