Skip to content

Commit d584b96

Browse files
author
feiweiwei
committed
增加统一异常处理功能、自定义异常类
1 parent 43fa0a2 commit d584b96

5 files changed

Lines changed: 164 additions & 1 deletion

File tree

src/main/java/com/monkey01/common/domain/MkResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.monkey01.common.domain;
22

3+
import com.monkey01.common.exception.ResultCode;
34
import io.swagger.annotations.ApiModel;
45

56
import java.util.HashMap;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.monkey01.common.exception;
2+
3+
/**
4+
* 自定义异常
5+
*
6+
* @author feiweiwei
7+
* @date 2016年10月27日 下午10:11:27
8+
*/
9+
public class MkException extends RuntimeException {
10+
private static final long serialVersionUID = 1L;
11+
12+
private String returnMsg;
13+
/**
14+
* returnCode 表示HOP内部错误码
15+
*/
16+
private String returnCode = "999999";
17+
private String errorMsg;
18+
19+
/**
20+
* errorCode 表示后台系统返回错误码
21+
*/
22+
private String errorCode = "999999";
23+
24+
public MkException(String returnMsg) {
25+
super(returnMsg);
26+
this.returnMsg = returnMsg;
27+
}
28+
29+
public MkException(String returnMsg, Throwable e) {
30+
super(returnMsg, e);
31+
this.returnMsg = returnMsg;
32+
}
33+
34+
public MkException(String returnMsg, String returnCode) {
35+
super(returnMsg);
36+
this.returnMsg = returnMsg;
37+
this.returnCode = returnCode;
38+
}
39+
40+
public MkException(String returnMsg, String returnCode, Throwable e) {
41+
super(returnMsg, e);
42+
this.returnMsg = returnMsg;
43+
this.returnCode = returnCode;
44+
}
45+
46+
public MkException(String returnMsg, String returnCode, String errorMsg, String errorCode) {
47+
super(returnMsg);
48+
this.returnMsg = returnMsg;
49+
this.returnCode = returnCode;
50+
this.errorMsg = errorMsg;
51+
this.errorCode = errorCode;
52+
}
53+
54+
public MkException(String returnMsg, String returnCode, String errorMsg, String errorCode, Throwable e) {
55+
super(returnMsg, e);
56+
this.returnMsg = returnMsg;
57+
this.returnCode = returnCode;
58+
this.errorMsg = errorMsg;
59+
this.errorCode = errorCode;
60+
}
61+
62+
public MkException(ResultCode resultCode, Throwable e) {
63+
super(resultCode.getMessage(), e);
64+
65+
this.errorMsg = resultCode.getMessage();
66+
this.errorCode = resultCode.getCode();
67+
}
68+
69+
public MkException(ResultCode resultCode) {
70+
this.errorMsg = resultCode.getMessage();
71+
this.errorCode = resultCode.getCode();
72+
}
73+
74+
public String getReturnMsg() {
75+
return returnMsg;
76+
}
77+
78+
public void setReturnMsg(String returnMsg) {
79+
this.returnMsg = returnMsg;
80+
}
81+
82+
public String getReturnCode() {
83+
return returnCode;
84+
}
85+
86+
public void setReturnCode(String returnCode) {
87+
this.returnCode = returnCode;
88+
}
89+
90+
public String getErrorMsg() {
91+
return errorMsg;
92+
}
93+
94+
public void setErrorMsg(String errorMsg) {
95+
this.errorMsg = errorMsg;
96+
}
97+
98+
public String getErrorCode() {
99+
return errorCode;
100+
}
101+
102+
public void setErrorCode(String errorCode) {
103+
this.errorCode = errorCode;
104+
}
105+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.monkey01.common.exception;
2+
3+
4+
import com.monkey01.common.domain.MkResponse;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.bind.annotation.RestControllerAdvice;
9+
import org.springframework.web.servlet.NoHandlerFoundException;
10+
11+
import java.util.Date;
12+
13+
/**
14+
* 异常处理器
15+
*
16+
* @author feiweiwei
17+
* @date 2016年10月27日 下午10:16:19
18+
*/
19+
@RestControllerAdvice
20+
public class MkExceptionHandler {
21+
private Logger logger = LoggerFactory.getLogger(getClass());
22+
23+
private final static String NO_URL_FOUND = "404";
24+
25+
/**
26+
* 处理自定义异常
27+
*/
28+
@ExceptionHandler(MkException.class)
29+
public MkResponse handleHOPException(MkException e){
30+
MkResponse r = new MkResponse();
31+
r.put("returnCode", e.getReturnCode());
32+
r.put("returnMsg", e.getReturnMsg());
33+
r.put("errorCode", e.getErrorCode());
34+
r.put("errorMsg", e.getErrorMsg());
35+
logger.error(new Date() + ": " + e.getMessage(), e);
36+
return r;
37+
}
38+
39+
@ExceptionHandler(NoHandlerFoundException.class)
40+
public MkResponse handlerNoFoundException(Exception e) {
41+
logger.error(new Date() + ": " + e.getMessage(), e);
42+
return MkResponse.error(NO_URL_FOUND, "路径不存在,请检查路径是否正确");
43+
}
44+
45+
46+
@ExceptionHandler(Exception.class)
47+
public MkResponse handleException(Exception e){
48+
logger.error(new Date() + ": " + e.getMessage(), e);
49+
return MkResponse.error(e.getMessage());
50+
}
51+
}

src/main/java/com/monkey01/common/domain/ResultCode.java renamed to src/main/java/com/monkey01/common/exception/ResultCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.monkey01.common.domain;
1+
package com.monkey01.common.exception;
22

33

44
/**

src/main/java/com/monkey01/modules/controller/TestController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.monkey01.common.annotation.MkParams;
44
import com.monkey01.common.annotation.SysLog;
55
import com.monkey01.common.domain.MkRequest;
6+
import com.monkey01.common.exception.MkException;
67
import org.springframework.web.bind.annotation.PostMapping;
78
import org.springframework.web.bind.annotation.RequestBody;
89
import org.springframework.web.bind.annotation.RestController;
@@ -29,4 +30,9 @@ public String testDesController(@MkParams MkRequest data){
2930

3031
return data.getSign() + data.getData().get("name");
3132
}
33+
34+
@PostMapping("/exception")
35+
public String exceptionController(){
36+
throw new MkException("系统异常", "990001");
37+
}
3238
}

0 commit comments

Comments
 (0)