1+ package io .github .opensabre .webmvc .exception ;
2+
3+ import io .github .opensabre .common .core .entity .vo .Result ;
4+ import io .github .opensabre .common .core .exception .SystemErrorType ;
5+ import jakarta .servlet .ServletException ;
6+ import lombok .extern .slf4j .Slf4j ;
7+ import org .springframework .core .annotation .Order ;
8+ import org .springframework .http .HttpStatus ;
9+ import org .springframework .http .converter .HttpMessageNotReadableException ;
10+ import org .springframework .web .HttpMediaTypeNotSupportedException ;
11+ import org .springframework .web .HttpRequestMethodNotSupportedException ;
12+ import org .springframework .web .bind .MethodArgumentNotValidException ;
13+ import org .springframework .web .bind .MissingServletRequestParameterException ;
14+ import org .springframework .web .bind .annotation .ExceptionHandler ;
15+ import org .springframework .web .bind .annotation .ResponseStatus ;
16+ import org .springframework .web .bind .annotation .RestControllerAdvice ;
17+ import org .springframework .web .method .annotation .MethodArgumentTypeMismatchException ;
18+ import org .springframework .web .multipart .MultipartException ;
19+ import org .springframework .web .servlet .NoHandlerFoundException ;
20+ import org .springframework .web .servlet .resource .NoResourceFoundException ;
21+
22+ /**
23+ * 默认全局异常处理类
24+ */
25+ @ Slf4j
26+ @ Order
27+ @ RestControllerAdvice
28+ public class DefaultWebMvcExceptionHandlerAdvice {
29+
30+ @ ExceptionHandler (value = {MissingServletRequestParameterException .class })
31+ public Result <?> missingServletRequestParameterException (MissingServletRequestParameterException ex ) {
32+ log .warn ("missing servlet request parameter exception:{}" , ex .getMessage ());
33+ return Result .fail (SystemErrorType .ARGUMENT_NOT_VALID );
34+ }
35+
36+ @ ExceptionHandler (value = {MethodArgumentNotValidException .class })
37+ public Result <?> argumentInvalidException (MethodArgumentNotValidException ex ) {
38+ log .warn ("service exception:{}" , ex .getMessage ());
39+ return Result .fail (SystemErrorType .ARGUMENT_NOT_VALID , ex .getBindingResult ().getFieldError ().getDefaultMessage ());
40+ }
41+
42+ @ ExceptionHandler (value = {HttpMessageNotReadableException .class , MethodArgumentTypeMismatchException .class })
43+ public Result <?> httpMessageConvertException (HttpMessageNotReadableException ex ) {
44+ log .warn ("http message convert exception:{}" , ex .getMessage ());
45+ return Result .fail (SystemErrorType .ARGUMENT_NOT_VALID , "数据解析错误:" + ex .getMessage ());
46+ }
47+
48+ @ ExceptionHandler (value = {MultipartException .class })
49+ public Result <?> uploadFileLimitException (MultipartException ex ) {
50+ log .warn ("upload file size limit:{}" , ex .getMessage ());
51+ return Result .fail (SystemErrorType .UPLOAD_FILE_SIZE_LIMIT );
52+ }
53+
54+ @ ExceptionHandler (value = {HttpRequestMethodNotSupportedException .class })
55+ @ ResponseStatus (HttpStatus .METHOD_NOT_ALLOWED )
56+ public Result <?> notSupportedMethodException (HttpRequestMethodNotSupportedException ex ) {
57+ log .warn ("http request method not supported exception {}" , ex .getMessage ());
58+ return Result .fail (SystemErrorType .METHOD_NOT_SUPPORTED );
59+ }
60+
61+ @ ExceptionHandler (value = {NoHandlerFoundException .class , NoResourceFoundException .class })
62+ @ ResponseStatus (HttpStatus .NOT_FOUND )
63+ public Result <?> noHandlerFoundException (ServletException ex ) {
64+ log .warn ("No static resource exception:{}" , ex .getMessage ());
65+ return Result .fail (SystemErrorType .RESOURCE_NOT_FOUND , ex .getMessage ());
66+ }
67+
68+ @ ExceptionHandler (value = {HttpMediaTypeNotSupportedException .class })
69+ @ ResponseStatus (HttpStatus .UNSUPPORTED_MEDIA_TYPE )
70+ public Result <?> notSupportedMethodException (HttpMediaTypeNotSupportedException ex ) {
71+ log .warn ("http request media not supported exception {}" , ex .getMessage ());
72+ return Result .fail (SystemErrorType .METHOD_NOT_SUPPORTED );
73+ }
74+ }
0 commit comments