Skip to content

Commit 5a64ce0

Browse files
committed
修改部分代码
1 parent f0c3088 commit 5a64ce0

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

docs/advanced/springboot-handle-exception-plus.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import org.springframework.http.HttpStatus;
3535

3636

3737
public enum ErrorCode {
38+
3839
RESOURCE_NOT_FOUND(1001, HttpStatus.NOT_FOUND, "未找到该资源"),
3940
REQUEST_VALIDATION_FAILED(1002, HttpStatus.BAD_REQUEST, "请求数据格式验证失败");
4041
private final int code;
@@ -127,38 +128,27 @@ public class ErrorReponse {
127128
**`BaseException.java`(继承自 `RuntimeException` 的抽象类,可以看做系统中其他异常类的父类)**
128129

129130
```java
130-
import org.springframework.util.ObjectUtils;
131131

132-
import java.util.HashMap;
133-
import java.util.Map;
134-
135-
/**
136-
* @author shuang.kou
137-
*/
138132
public abstract class BaseException extends RuntimeException {
139133
private final ErrorCode error;
140134
private final HashMap<String, Object> data = new HashMap<>();
141135

142136
public BaseException(ErrorCode error, Map<String, Object> data) {
143-
super(format(error.getCode(), error.getMessage(), data));
137+
super(error.getMessage());
144138
this.error = error;
145139
if (!ObjectUtils.isEmpty(data)) {
146140
this.data.putAll(data);
147141
}
148142
}
149143

150144
protected BaseException(ErrorCode error, Map<String, Object> data, Throwable cause) {
151-
super(format(error.getCode(), error.getMessage(), data), cause);
145+
super(error.getMessage(), cause);
152146
this.error = error;
153147
if (!ObjectUtils.isEmpty(data)) {
154148
this.data.putAll(data);
155149
}
156150
}
157151

158-
private static String format(Integer code, String message, Map<String, Object> data) {
159-
return String.format("[%d]%s:%s.", code, message, ObjectUtils.isEmpty(data) ? "" : data.toString());
160-
}
161-
162152
public ErrorCode getError() {
163153
return error;
164154
}
@@ -190,21 +180,25 @@ public class ResourceNotFoundException extends BaseException {
190180
`@ExceptionHandler` 捕获异常的过程中,会优先找到最匹配的。
191181

192182
```java
193-
/**
194-
* @author shuang.kou
195-
*/
183+
import com.twuc.webApp.web.ExceptionController;
184+
import org.springframework.http.HttpHeaders;
185+
import org.springframework.http.HttpStatus;
186+
import org.springframework.http.ResponseEntity;
187+
import org.springframework.web.bind.annotation.ControllerAdvice;
188+
import org.springframework.web.bind.annotation.ExceptionHandler;
189+
import org.springframework.web.bind.annotation.ResponseBody;
190+
import javax.servlet.http.HttpServletRequest;
191+
196192
@ControllerAdvice(assignableTypes = {ExceptionController.class})
197193
@ResponseBody
198194
public class GlobalExceptionHandler {
199195

200196
@ExceptionHandler(BaseException.class)
201197
public ResponseEntity<?> handleAppException(BaseException ex, HttpServletRequest request) {
202-
String path = request.getRequestURI();
203-
ErrorReponse representation = new ErrorReponse(ex, path);
198+
ErrorReponse representation = new ErrorReponse(ex, request.getRequestURI());
204199
return new ResponseEntity<>(representation, new HttpHeaders(), ex.getError().getStatus());
205200
}
206201

207-
208202
@ExceptionHandler(value = ResourceNotFoundException.class)
209203
public ResponseEntity<ErrorReponse> handleResourceNotFoundException(ResourceNotFoundException ex, HttpServletRequest request) {
210204
ErrorReponse errorReponse = new ErrorReponse(ex, request.getRequestURI());

source-code/basis/springboot-handle-exception-improved/src/main/java/com/twuc/webApp/exception/BaseException.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,21 @@ public abstract class BaseException extends RuntimeException {
1313
private final HashMap<String, Object> data = new HashMap<>();
1414

1515
public BaseException(ErrorCode error, Map<String, Object> data) {
16-
super(format(error.getCode(), error.getMessage(), data));
16+
super(error.getMessage());
1717
this.error = error;
1818
if (!ObjectUtils.isEmpty(data)) {
1919
this.data.putAll(data);
2020
}
2121
}
2222

2323
protected BaseException(ErrorCode error, Map<String, Object> data, Throwable cause) {
24-
super(format(error.getCode(), error.getMessage(), data), cause);
24+
super(error.getMessage(), cause);
2525
this.error = error;
2626
if (!ObjectUtils.isEmpty(data)) {
2727
this.data.putAll(data);
2828
}
2929
}
3030

31-
private static String format(Integer code, String message, Map<String, Object> data) {
32-
return String.format("[%d]%s:%s.", code, message, ObjectUtils.isEmpty(data) ? "" : data.toString());
33-
}
34-
3531
public ErrorCode getError() {
3632
return error;
3733
}

source-code/basis/springboot-handle-exception-improved/src/main/java/com/twuc/webApp/exception/GlobalExceptionHandler.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ public class GlobalExceptionHandler {
2020

2121
@ExceptionHandler(BaseException.class)
2222
public ResponseEntity<?> handleAppException(BaseException ex, HttpServletRequest request) {
23-
String path = request.getRequestURI();
24-
ErrorReponse representation = new ErrorReponse(ex, path);
23+
ErrorReponse representation = new ErrorReponse(ex, request.getRequestURI());
2524
return new ResponseEntity<>(representation, new HttpHeaders(), ex.getError().getStatus());
2625
}
2726

28-
2927
@ExceptionHandler(value = ResourceNotFoundException.class)
3028
public ResponseEntity<ErrorReponse> handleResourceNotFoundException(ResourceNotFoundException ex, HttpServletRequest request) {
3129
ErrorReponse errorReponse = new ErrorReponse(ex, request.getRequestURI());

0 commit comments

Comments
 (0)