Skip to content

Commit 83aaf22

Browse files
统一捕获异常
1 parent a375518 commit 83aaf22

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.common;
2+
3+
/**
4+
* Created by zhuzhengping on 2017/3/21.
5+
*/
6+
public class BootException extends RuntimeException {
7+
8+
private Integer code;
9+
10+
public BootException(ExceptionEnum exceptionEnum) {
11+
super(exceptionEnum.getMsg());
12+
this.code = exceptionEnum.getCode();
13+
}
14+
15+
public Integer getCode() {
16+
return code;
17+
}
18+
19+
public void setCode(Integer code) {
20+
this.code = code;
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.common;
2+
3+
/**
4+
* Created by zhuzhengping on 2017/3/21.
5+
*/
6+
public enum ExceptionEnum {
7+
UNKONW_ERROR(-1,"未知错误"),
8+
SUCCESS(0,"成功"),
9+
;
10+
11+
private Integer code;
12+
13+
private String msg;
14+
15+
ExceptionEnum(Integer code, String msg) {
16+
this.code = code;
17+
this.msg = msg;
18+
}
19+
20+
public Integer getCode() {
21+
return code;
22+
}
23+
24+
public String getMsg() {
25+
return msg;
26+
}
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.common;
2+
3+
import com.compont.Result;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.web.bind.annotation.ControllerAdvice;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.bind.annotation.ResponseBody;
9+
10+
/**
11+
* Created by zhuzhengping on 2017/3/21.
12+
*/
13+
14+
@ControllerAdvice
15+
public class ExceptionHandle {
16+
17+
private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandle.class);
18+
19+
20+
@ExceptionHandler(value = Exception.class)
21+
@ResponseBody
22+
public Result catchFromService(Exception e){
23+
if(e instanceof BootException){
24+
BootException bootException = (BootException) e;
25+
return ResultUtil.error(100,e.getMessage());
26+
}
27+
LOGGER.error("【系统异常】{}",e);
28+
return ResultUtil.error(ExceptionEnum.UNKONW_ERROR);
29+
}
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.common;
2+
3+
import com.compont.Result;
4+
5+
/**
6+
* Created by zhuzhengping on 2017/3/21.
7+
* resultutil to form the result
8+
*/
9+
public class ResultUtil {
10+
11+
public static Result success(Object object){
12+
Result result = new Result();
13+
result.setCode(0);
14+
result.setMsg("success");
15+
result.getData(object);
16+
return result;
17+
}
18+
19+
public static Result success(){
20+
return success(null);
21+
}
22+
23+
public static Result error(Integer code,String msg){
24+
Result result = new Result();
25+
result.setCode(code);
26+
result.setMsg(msg);
27+
result.setData(null);
28+
return result;
29+
}
30+
31+
public static Result error(ExceptionEnum exceptionEnum){
32+
Result result = new Result();
33+
result.setCode(exceptionEnum.getCode());
34+
result.setMsg(exceptionEnum.getMsg());
35+
result.setData(null);
36+
return result;
37+
}
38+
}

src/main/java/com/compont/Result.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.compont;
2+
3+
/**
4+
* Created by zhuzhengping on 2017/3/21.
5+
*/
6+
public class Result<T> {
7+
8+
// error_code
9+
private Integer code;
10+
11+
// error_msg
12+
private String msg;
13+
14+
// content
15+
private T data;
16+
17+
public Integer getCode() {
18+
return code;
19+
}
20+
21+
public void setCode(Integer code) {
22+
this.code = code;
23+
}
24+
25+
public String getMsg() {
26+
return msg;
27+
}
28+
29+
public void setMsg(String msg) {
30+
this.msg = msg;
31+
}
32+
33+
public T getData(Object object) {
34+
return data;
35+
}
36+
37+
public void setData(T data) {
38+
this.data = data;
39+
}
40+
}

0 commit comments

Comments
 (0)