|
|
@ -107,19 +107,24 @@ public class GlobalExceptionHandler { |
|
|
|
|
|
|
|
|
|
|
|
// 捕获自定义业务异常 |
|
|
|
// @ExceptionHandler(BusinessException.class) |
|
|
|
// public ResponseEntity<Result> handleBusinessException(BusinessException ex, HttpServletRequest request) { |
|
|
|
// HttpHeaders headers = new HttpHeaders(); |
|
|
|
// headers.set("Access-Control-Allow-Origin", "*"); // 或指定域名 |
|
|
|
// headers.set("Access-Control-Allow-Credentials", "true"); |
|
|
|
// // 构建统一的错误响应 |
|
|
|
// Result<Void> errorResult = Result.failure(ex.getCode(), ex.getMessage()); |
|
|
|
// |
|
|
|
// |
|
|
|
// return ResponseEntity |
|
|
|
// .status(ex.getCode()) // 使用 BusinessException 中的状态码 |
|
|
|
// .headers(headers) |
|
|
|
// .body(errorResult); // 返回 Result 作为响应体 |
|
|
|
// } |
|
|
|
// 捕获自定义业务异常 |
|
|
|
@ExceptionHandler(BusinessException.class) |
|
|
|
public ResponseEntity<Result> handleBusinessException(BusinessException ex, HttpServletRequest request) { |
|
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
|
headers.set("Access-Control-Allow-Origin", "*"); // 或指定域名 |
|
|
|
headers.set("Access-Control-Allow-Credentials", "true"); |
|
|
|
// 构建统一的错误响应 |
|
|
|
Result<Void> errorResult = Result.failure(ex.getCode(), ex.getMessage()); |
|
|
|
|
|
|
|
|
|
|
|
return ResponseEntity |
|
|
|
.status(ex.getCode()) // 使用 BusinessException 中的状态码 |
|
|
|
.headers(headers) |
|
|
|
.body(errorResult); // 返回 Result 作为响应体 |
|
|
|
public Result<Void> handleBusinessException(BusinessException e) { |
|
|
|
return Result.failure(e.getCode(), e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|