99import org .springframework .web .bind .annotation .ExceptionHandler ;
1010import org .springframework .web .bind .annotation .ResponseBody ;
1111import org .springframework .web .context .request .WebRequest ;
12+ import top .cadecode .common .constant .ResCode ;
1213import top .cadecode .common .response .SimpleRes ;
13- import top .cadecode .common .constant .ErrorEnum ;
14- import top .cadecode .common .constant .StatusEnum ;
1514import top .cadecode .common .util .MapUtil ;
16- import top .cadecode .common .response . SimpleRes . ResError ;
15+ import top .cadecode .common .util . MapUtil . MapBuilder ;
1716
1817import javax .servlet .http .HttpServletRequest ;
19- import javax .servlet .http .HttpServletResponse ;
2018import java .util .Map ;
2119
2220/**
@@ -33,69 +31,58 @@ public class SimpleExceptionHandler extends DefaultErrorAttributes {
3331 */
3432 @ ExceptionHandler (SimpleException .class )
3533 @ ResponseBody
36- public SimpleRes handleSimpleException (SimpleException e , HttpServletRequest request ,
37- HttpServletResponse response ) {
34+ public SimpleRes <?> handleSimpleException (SimpleException e , HttpServletRequest request ) {
3835 log .error ("SimpleException Handler =>" , e );
39- return SimpleRes .fail (e .getErrorEnum ())
40- .path ( request )
41- .status ( response );
36+ return SimpleRes .of (e .getResCode ())
37+ .errorMsg ( e . getMessage () )
38+ .path ( request . getRequestURI () );
4239 }
4340
4441 /**
4542 * 处理一般异常
4643 */
4744 @ ExceptionHandler (Exception .class )
4845 @ ResponseBody
49- public SimpleRes handleOtherException (Exception e , HttpServletRequest request ,
50- HttpServletResponse response ) {
46+ public SimpleRes <?> handleOtherException (Exception e , HttpServletRequest request ) {
47+ // 获取请求路径
48+ String requestURI = request .getRequestURI ();
5149 log .error ("Exception Handler =>" , e );
5250 // 处理 SpringMVC 异常
5351 if (e instanceof MissingServletRequestParameterException ) {
54- return SimpleRes .fail (ErrorEnum .REQ_PARAM_INVALID )
55- .path (request )
56- .status (response );
52+ return SimpleRes .of (ResCode .REQ_PARAM_INVALID )
53+ .path (requestURI );
5754 }
5855 if (e instanceof HttpMessageNotReadableException ) {
59- return SimpleRes .fail (ErrorEnum .REQ_BODY_INVALID )
60- .path (request )
61- .status (response );
56+ return SimpleRes .of (ResCode .REQ_BODY_INVALID )
57+ .path (requestURI );
6258 }
63- return SimpleRes .fail (ErrorEnum .UNKNOWN )
64- .path (request )
65- .status (response );
59+ return SimpleRes .of (ResCode .UNKNOWN )
60+ .path (requestURI );
6661 }
6762
6863 /**
6964 * 自定义 SpringMVC 404、405 返回内容
7065 */
71- private static final String STATUS_KEY = "status" ;
72- private static final String MESSAGE_KEY = "message" ;
73- private static final String PATH_KEY = "path" ;
74- private static final String ERROR_KEY = "error" ;
75-
7666 @ Override
7767 public Map <String , Object > getErrorAttributes (WebRequest webRequest , ErrorAttributeOptions options ) {
7868 // 获取填充好的 errorAttributes
7969 Map <String , Object > errorAttributes = super .getErrorAttributes (webRequest , options );
80- // 获取状态码与路径
81- int status = (int ) errorAttributes .get (STATUS_KEY );
82- String path = (String ) errorAttributes .get (PATH_KEY );
70+ // 获取请求路径
71+ int status = (int ) errorAttributes .get ("status" );
72+ String path = (String ) errorAttributes .get ("path" );
73+ MapBuilder <String , Object > resMapBuilder = MapUtil .create ().add ("path" , path );
8374 // 处理 404
84- if (status == StatusEnum .NOT_EXIT .getStatus ()) {
85- return MapUtil .create ()
86- .add (STATUS_KEY , StatusEnum .NOT_EXIT .getStatus ())
87- .add (MESSAGE_KEY , StatusEnum .NOT_EXIT .getMessage ())
88- .add (PATH_KEY , path )
89- .add (ERROR_KEY , new ResError (ErrorEnum .API_NOT_EXIST ))
75+ if (status == 404 ) {
76+ return resMapBuilder
77+ .add ("code" , ResCode .REQ_PATH_NOT_EXIST .getCode ())
78+ .add ("reason" , ResCode .REQ_PATH_NOT_EXIST .getReason ())
9079 .asMap ();
9180 }
9281 // 处理 405
93- if (status == StatusEnum .NOT_FIT .getStatus ()) {
94- return MapUtil .create ()
95- .add (STATUS_KEY , StatusEnum .NOT_FIT .getStatus ())
96- .add (MESSAGE_KEY , StatusEnum .NOT_FIT .getMessage ())
97- .add (PATH_KEY , path )
98- .add (ERROR_KEY , new ResError (ErrorEnum .REQ_METHOD_INVALID ))
82+ if (status == 405 ) {
83+ return resMapBuilder
84+ .add ("code" , ResCode .REQ_METHOD_INVALID .getCode ())
85+ .add ("reason" , ResCode .REQ_METHOD_INVALID .getReason ())
9986 .asMap ();
10087 }
10188 return errorAttributes ;
0 commit comments