Skip to content

Commit fc227f0

Browse files
committed
异常体系重构
1 parent 7622eda commit fc227f0

File tree

9 files changed

+87
-52
lines changed

9 files changed

+87
-52
lines changed

core-web/src/main/java/lazy/fast/code/core/web/exception/GlobalRestExceptionHandler.java renamed to core-web/src/main/java/lazy/fast/code/core/web/exception/AbstractRestExceptionHandler.java

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,56 +15,46 @@
1515
import org.springframework.web.bind.MethodArgumentNotValidException;
1616
import org.springframework.web.bind.MissingServletRequestParameterException;
1717
import org.springframework.web.bind.annotation.ExceptionHandler;
18-
import org.springframework.web.bind.annotation.RestControllerAdvice;
1918
import org.springframework.web.client.RestClientException;
2019

2120
import java.util.List;
2221

2322
/**
24-
* 全局异常处理器 - 只针对返回JSON数据交互格式(标记为@RestController类)
23+
* 全局异常处理器 - 只针对返回JSON数据交互格式(标记为@RestController类) <br />
24+
* 应用应该创建一个自定义全局异常处理器,并继承此类,让此类定义的ExceptionHandler生效,如:
25+
* <pre>
26+
* &#64;RestControllerAdvice
27+
* public class GlobalRestExceptionHandler extends AbstractRestExceptionHandler {
28+
*
29+
* }
30+
* </pre>
2531
*
2632
* @author wendell
2733
*/
2834
@Slf4j
29-
@RestControllerAdvice
30-
public class GlobalRestExceptionHandler {
35+
public abstract class AbstractRestExceptionHandler {
3136

3237
@ExceptionHandler(value = NoContentNotException.class)
3338
public ResponseEntity<Void> notContentNotExceptionHandler() {
3439
return ResponseEntity.noContent().build();
3540
}
3641

37-
@ExceptionHandler(value = BusinessException.class)
38-
public ResponseEntity<ResultMsg> businessExceptionHandler(BusinessException e) {
39-
return ResponseEntity.badRequest().body(e.getResultMsg());
40-
}
41-
42-
@ExceptionHandler(value = AuthException.class)
43-
public ResponseEntity<ResultMsg> authExceptionHandler(AuthException e) {
44-
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getResultMsg());
45-
}
46-
47-
@ExceptionHandler(value = ForbiddenException.class)
48-
public ResponseEntity<ResultMsg> forbiddenExceptionHandler(ForbiddenException e) {
49-
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getResultMsg());
50-
}
51-
52-
@ExceptionHandler(value = NotFoundException.class)
53-
public ResponseEntity<ResultMsg> notFoundExceptionHandler(NotFoundException e) {
54-
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getResultMsg());
55-
}
56-
5742
@ExceptionHandler(value = SystemException.class)
5843
public ResponseEntity<ResultMsg> systemExceptionHandler(SystemException e) {
5944
log.error(e.getMessage(), e);
60-
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultMsg.error());
45+
return baseUnCheckHandler(e);
46+
}
47+
48+
@ExceptionHandler(value = BaseUnCheckException.class)
49+
public ResponseEntity<ResultMsg> baseUnCheckHandler(BaseUnCheckException e) {
50+
return ResponseEntity.status(e.getHttpStatus()).body(e.getResultMsg());
6151
}
6252

6353
@ExceptionHandler(value = MethodArgumentNotValidException.class)
6454
public ResponseEntity<ResultMsg> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
6555
BindingResult bindingResult = e.getBindingResult();
6656
List<ObjectError> allErrors = bindingResult.getAllErrors();
67-
StringBuilder sb = new StringBuilder(10);
57+
StringBuilder sb = new StringBuilder(32);
6858
allErrors.forEach(oe -> sb.append(oe.getDefaultMessage()).append(";"));
6959
String body = sb.toString();
7060
if (body.endsWith(";")) {
@@ -125,10 +115,4 @@ public ResponseEntity<ResultMsg> exceptionHandler(Exception e) {
125115
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultMsg.error());
126116
}
127117

128-
@ExceptionHandler(value = BaseUnCheckException.class)
129-
public ResponseEntity<ResultMsg> baseUnCheckHandler(BaseUnCheckException e) {
130-
// 其它继承BaseUnCheckException的自定义非受检异常的处理方式
131-
return ResponseEntity.status(e.getResultMsg().getCode()).body(e.getResultMsg());
132-
}
133-
134118
}

core-web/src/main/java/lazy/fast/code/core/web/exception/AuthException.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package lazy.fast.code.core.web.exception;
22

3+
import org.springframework.http.HttpStatus;
4+
35
import lazy.fast.code.core.web.result.MsgEnum;
46
import lazy.fast.code.core.web.result.MsgEnumable;
57
import lazy.fast.code.core.web.result.ResultMsg;
@@ -11,11 +13,13 @@
1113
*/
1214
public class AuthException extends BaseUnCheckException {
1315

16+
private static final HttpStatus HTTP_STATUS = HttpStatus.UNAUTHORIZED;
17+
1418
/**
1519
* {"code":401,"msg":"未经过身份认证"}
1620
*/
1721
public AuthException() {
18-
super(ResultMsg.of(MsgEnum.AUTH_ERROR));
22+
super(HTTP_STATUS, ResultMsg.of(MsgEnum.AUTH_ERROR));
1923
}
2024

2125
/**
@@ -25,7 +29,7 @@ public AuthException() {
2529
* 异常简要信息
2630
*/
2731
public AuthException(String message) {
28-
super(ResultMsg.of(MsgEnum.AUTH_ERROR.code(), message));
32+
super(HTTP_STATUS, ResultMsg.of(MsgEnum.AUTH_ERROR.code(), message));
2933
}
3034

3135
/**
@@ -37,7 +41,7 @@ public AuthException(String message) {
3741
* 消息枚举
3842
*/
3943
public AuthException(MsgEnumable msgEnum) {
40-
super(ResultMsg.of(msgEnum));
44+
super(HTTP_STATUS, ResultMsg.of(msgEnum));
4145
}
4246

4347
}

core-web/src/main/java/lazy/fast/code/core/web/exception/BaseUnCheckException.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22

33
import lazy.fast.code.core.web.result.ResultMsg;
44
import lombok.Getter;
5+
import lombok.Setter;
6+
import org.springframework.http.HttpStatus;
57

68
/**
79
* 基础异常类 - 自定义的非受检异常应该继承此类
810
*
911
* @author wendell
1012
*/
1113
@Getter
14+
@Setter
1215
public abstract class BaseUnCheckException extends RuntimeException {
1316

17+
/**
18+
* http状态码, ResultMsg中的code可以与此相同
19+
*/
20+
private HttpStatus httpStatus;
21+
1422
private ResultMsg resultMsg;
1523

16-
public BaseUnCheckException(ResultMsg resultMsg) {
17-
super(resultMsg.getMsg());
24+
public BaseUnCheckException(HttpStatus httpStatus, ResultMsg resultMsg) {
25+
super(resultMsg.toString());
26+
this.httpStatus = httpStatus;
1827
this.resultMsg = resultMsg;
1928
}
2029

core-web/src/main/java/lazy/fast/code/core/web/exception/BusinessException.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lazy.fast.code.core.web.result.MsgEnumable;
44
import lazy.fast.code.core.web.result.ResultMsg;
5+
import org.springframework.http.HttpStatus;
56

67
/**
78
* 自定义业务异常类 - HTTP Status 400
@@ -10,11 +11,13 @@
1011
*/
1112
public class BusinessException extends BaseUnCheckException {
1213

14+
private static final HttpStatus HTTP_STATUS = HttpStatus.BAD_REQUEST;
15+
1316
/**
1417
* {"code":400,"msg":"操作失败"}
1518
*/
1619
public BusinessException() {
17-
super(ResultMsg.fail());
20+
super(HTTP_STATUS, ResultMsg.fail());
1821
}
1922

2023
/**
@@ -24,7 +27,7 @@ public BusinessException() {
2427
* 异常简要信息
2528
*/
2629
public BusinessException(String message) {
27-
super(ResultMsg.fail(message));
30+
super(HTTP_STATUS, ResultMsg.fail(message));
2831
}
2932

3033
/**
@@ -36,7 +39,7 @@ public BusinessException(String message) {
3639
* 异常详细信息
3740
*/
3841
public BusinessException(String message, String detailMsg) {
39-
super(ResultMsg.fail(message, detailMsg));
42+
super(HTTP_STATUS, ResultMsg.fail(message, detailMsg));
4043
}
4144

4245
/**
@@ -48,7 +51,7 @@ public BusinessException(String message, String detailMsg) {
4851
* 消息枚举
4952
*/
5053
public BusinessException(MsgEnumable msgEnum) {
51-
super(ResultMsg.of(msgEnum));
54+
super(HTTP_STATUS, ResultMsg.of(msgEnum));
5255
}
5356

5457
}

core-web/src/main/java/lazy/fast/code/core/web/exception/ForbiddenException.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package lazy.fast.code.core.web.exception;
22

3+
import org.springframework.http.HttpStatus;
4+
35
import lazy.fast.code.core.web.result.MsgEnum;
46
import lazy.fast.code.core.web.result.MsgEnumable;
57
import lazy.fast.code.core.web.result.ResultMsg;
@@ -11,11 +13,13 @@
1113
*/
1214
public class ForbiddenException extends BaseUnCheckException {
1315

16+
private static final HttpStatus HTTP_STATUS = HttpStatus.FORBIDDEN;
17+
1418
/**
1519
* {"code":403,"msg":"无访问权限"}
1620
*/
1721
public ForbiddenException() {
18-
super(ResultMsg.of(MsgEnum.FORBIDDEN));
22+
super(HTTP_STATUS, ResultMsg.of(MsgEnum.FORBIDDEN));
1923
}
2024

2125
/**
@@ -25,7 +29,7 @@ public ForbiddenException() {
2529
* 异常简要信息
2630
*/
2731
public ForbiddenException(String message) {
28-
super(ResultMsg.of(MsgEnum.FORBIDDEN.code(), message));
32+
super(HTTP_STATUS, ResultMsg.of(MsgEnum.FORBIDDEN.code(), message));
2933
}
3034

3135
/**
@@ -37,7 +41,7 @@ public ForbiddenException(String message) {
3741
* 消息枚举
3842
*/
3943
public ForbiddenException(MsgEnumable msgEnum) {
40-
super(ResultMsg.of(msgEnum));
44+
super(HTTP_STATUS, ResultMsg.of(msgEnum));
4145
}
4246

4347
}

core-web/src/main/java/lazy/fast/code/core/web/exception/NotFoundException.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package lazy.fast.code.core.web.exception;
22

3+
import org.springframework.http.HttpStatus;
4+
35
import lazy.fast.code.core.web.result.MsgEnum;
46
import lazy.fast.code.core.web.result.MsgEnumable;
57
import lazy.fast.code.core.web.result.ResultMsg;
@@ -11,11 +13,13 @@
1113
*/
1214
public class NotFoundException extends BaseUnCheckException {
1315

16+
private static final HttpStatus HTTP_STATUS = HttpStatus.NOT_FOUND;
17+
1418
/**
1519
* {"code":404,"msg":"资源不存在"}
1620
*/
1721
public NotFoundException() {
18-
super(ResultMsg.of(MsgEnum.NOT_FOUND));
22+
super(HTTP_STATUS, ResultMsg.of(MsgEnum.NOT_FOUND));
1923
}
2024

2125
/**
@@ -25,7 +29,7 @@ public NotFoundException() {
2529
* 异常简要信息
2630
*/
2731
public NotFoundException(String message) {
28-
super(ResultMsg.of(MsgEnum.NOT_FOUND.code(), message));
32+
super(HTTP_STATUS, ResultMsg.of(MsgEnum.NOT_FOUND.code(), message));
2933
}
3034

3135
/**
@@ -37,7 +41,7 @@ public NotFoundException(String message) {
3741
* 消息枚举
3842
*/
3943
public NotFoundException(MsgEnumable msgEnum) {
40-
super(ResultMsg.of(msgEnum));
44+
super(HTTP_STATUS, ResultMsg.of(msgEnum));
4145
}
4246

4347
}

core-web/src/main/java/lazy/fast/code/core/web/exception/SystemException.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package lazy.fast.code.core.web.exception;
22

3+
import org.springframework.http.HttpStatus;
4+
35
import lazy.fast.code.core.web.result.MsgEnumable;
46
import lazy.fast.code.core.web.result.ResultMsg;
57

@@ -10,11 +12,13 @@
1012
*/
1113
public class SystemException extends BaseUnCheckException {
1214

15+
private static final HttpStatus HTTP_STATUS = HttpStatus.INTERNAL_SERVER_ERROR;
16+
1317
/**
1418
* {"code":500,"msg":"服务器异常,请稍后再试"}
1519
*/
1620
public SystemException() {
17-
super(ResultMsg.error());
21+
super(HTTP_STATUS, ResultMsg.error());
1822
}
1923

2024
/**
@@ -24,7 +28,7 @@ public SystemException() {
2428
* 异常简要信息
2529
*/
2630
public SystemException(String message) {
27-
super(ResultMsg.error(message));
31+
super(HTTP_STATUS, ResultMsg.error(message));
2832
}
2933

3034
/**
@@ -36,7 +40,7 @@ public SystemException(String message) {
3640
* 异常详细信息
3741
*/
3842
public SystemException(String message, String detailMsg) {
39-
super(ResultMsg.error(message, detailMsg));
43+
super(HTTP_STATUS, ResultMsg.error(message, detailMsg));
4044
}
4145

4246
/**
@@ -48,7 +52,7 @@ public SystemException(String message, String detailMsg) {
4852
* 消息枚举
4953
*/
5054
public SystemException(MsgEnumable msgEnum) {
51-
super(ResultMsg.of(msgEnum));
55+
super(HTTP_STATUS, ResultMsg.of(msgEnum));
5256
}
5357

5458
}

demo/src/main/java/lazy/fast/code/demo/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public ResponseEntity<Void> notFound() {
101101
*
102102
* @return {"code":404,"msg":"资源不存在"}
103103
*/
104-
@GetMapping("/not-found")
104+
@GetMapping("/not-found1")
105105
public ResponseEntity<ResultMsg> notFound1() {
106106
throw new NotFoundException();
107107
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package lazy.fast.code.demo.core;
2+
3+
import lazy.fast.code.core.web.exception.AbstractRestExceptionHandler;
4+
import lazy.fast.code.core.web.exception.NoContentNotException;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.ExceptionHandler;
7+
import org.springframework.web.bind.annotation.RestControllerAdvice;
8+
9+
/**
10+
* 全局异常处理器
11+
*
12+
* @author wendell
13+
*/
14+
@RestControllerAdvice
15+
public class GlobalRestExceptionHandler extends AbstractRestExceptionHandler {
16+
17+
@Override
18+
@ExceptionHandler(value = NoContentNotException.class)
19+
public ResponseEntity<Void> notContentNotExceptionHandler() {
20+
return ResponseEntity.noContent().build();
21+
}
22+
23+
}

0 commit comments

Comments
 (0)