33import lombok .extern .slf4j .Slf4j ;
44import org .springframework .boot .web .error .ErrorAttributeOptions ;
55import org .springframework .boot .web .servlet .error .DefaultErrorAttributes ;
6- import org .springframework .http .converter .HttpMessageNotReadableException ;
7- import org .springframework .web .bind .MissingServletRequestParameterException ;
86import org .springframework .web .bind .annotation .ControllerAdvice ;
97import org .springframework .web .bind .annotation .ExceptionHandler ;
108import org .springframework .web .bind .annotation .ResponseBody ;
119import org .springframework .web .context .request .WebRequest ;
1210import top .cadecode .simple .common .response .SimpleRes ;
13- import top .cadecode .simple .constant .ReasonEnum ;
11+ import top .cadecode .simple .constant .ErrorEnum ;
12+ import top .cadecode .simple .constant .StatusEnum ;
13+ import top .cadecode .simple .util .MapUtil ;
1414
1515import javax .servlet .http .HttpServletRequest ;
1616import javax .servlet .http .HttpServletResponse ;
@@ -28,44 +28,34 @@ public class SimpleExceptionHandler extends DefaultErrorAttributes {
2828 /**
2929 * 处理 SimpleException
3030 */
31- @ ExceptionHandler (value = SimpleException .class )
31+ @ ExceptionHandler (SimpleException .class )
3232 @ ResponseBody
3333 public SimpleRes handleSimpleException (SimpleException e , HttpServletRequest request ,
3434 HttpServletResponse response ) {
3535 log .error ("SimpleException Handler =>" , e );
36- response .setStatus (e .getCode ());
37- return SimpleRes .exception (e )
38- .path (getPath (request ))
39- .error (getErrorMsg (e ));
36+ return SimpleRes .fail (e .getErrorEnum ())
37+ .path (request )
38+ .status (response );
4039 }
4140
4241 /**
4342 * 处理一般异常
4443 */
45- @ ExceptionHandler (value = Exception .class )
44+ @ ExceptionHandler (Exception .class )
4645 @ ResponseBody
4746 public SimpleRes handleOtherException (Exception e , HttpServletRequest request ,
4847 HttpServletResponse response ) {
4948 log .error ("Exception Handler =>" , e );
50- // 判断是否为 SpringMVC 原生异常
51- if (e instanceof HttpMessageNotReadableException
52- || e instanceof MissingServletRequestParameterException ) {
53- response .setStatus (ReasonEnum .NOT_FIT .getCode ());
54- return SimpleRes .reason (ReasonEnum .NOT_FIT )
55- .path (getPath (request ))
56- .error (getErrorMsg (e ));
57- }
58- response .setStatus (ReasonEnum .FAIL .getCode ());
59- return SimpleRes .reason (ReasonEnum .FAIL )
60- .path (getPath (request ))
61- .error (getErrorMsg (e ));
49+ return SimpleRes .fail (ErrorEnum .UNKNOWN )
50+ .path (request )
51+ .status (response );
6252 }
6353
6454 /**
65- * 定制 404、405 等原生异常的返回内容
55+ * 自定义 SpringMVC 404 返回内容
6656 */
67-
6857 private static final String STATUS_KEY = "status" ;
58+ private static final String MESSAGE_KEY = "message" ;
6959 private static final String PATH_KEY = "path" ;
7060 private static final String ERROR_KEY = "error" ;
7161
@@ -78,35 +68,13 @@ public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttrib
7868 String path = (String ) errorAttributes .get (PATH_KEY );
7969 String error = (String ) errorAttributes .get (ERROR_KEY );
8070 // 处理 404 异常
81- if (status == ReasonEnum .NOT_EXIT .getCode ()) {
82- return SimpleRes .reason (ReasonEnum .NOT_EXIT )
83- .path (path )
84- .error (error )
85- .map ();
86- }
87- // 处理 405 异常
88- if (status == ReasonEnum .NOT_FIT .getCode ()) {
89- return SimpleRes .reason (ReasonEnum .NOT_FIT )
90- .path (path )
91- .error (error )
92- .map ();
71+ if (status == StatusEnum .NOT_EXIT .getStatus ()) {
72+ return MapUtil .create ()
73+ .add (STATUS_KEY , StatusEnum .NOT_EXIT .getStatus ())
74+ .add (MESSAGE_KEY , StatusEnum .NOT_EXIT .getMessage ())
75+ .add (PATH_KEY , path )
76+ .asMap ();
9377 }
94- return SimpleRes .reason (ReasonEnum .FAIL )
95- .path (path )
96- .map ();
97- }
98-
99- /**
100- * 获取请求路径
101- */
102- private String getPath (HttpServletRequest request ) {
103- return request .getRequestURI ();
104- }
105-
106- /**
107- * 获取异常信息
108- */
109- private String getErrorMsg (Exception e ) {
110- return "[" + e .getClass ().getName () + "]" + e .getMessage ();
78+ return errorAttributes ;
11179 }
11280}
0 commit comments