error data

This commit is contained in:
2026-08-19 14:37:55 +08:00
parent 1f7705546d
commit 72baf66d59
10 changed files with 54 additions and 29 deletions
@@ -1,6 +1,9 @@
package com.yunhai.bschclassmanage.controller.Handler;
import com.yunhai.platform.utils.exception.DException;
import com.yunhai.platform.utils.exception.ErrorCode;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
@@ -13,9 +16,24 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
public class ValidationExceptionHandler {
/**
* 兜底 DException:平台 GlobalExceptionHandler 只返回 code/msg,会丢掉 data。
* 本项目优先捕获,把业务异常里的 data(如导入错误列表)透传给前端。
*/
@ExceptionHandler(DException.class)
public ResponseEntity<Map<String, Object>> handleDException(DException ex) {
Map<String, Object> response = new LinkedHashMap<>();
response.put("code", ex.getCode() != null ? ex.getCode().getCode() : ErrorCode.CUSTOM.getCode());
response.put("msg", ex.getMessage());
response.put("data", ex.getData());
response.put("timeNow", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
return new ResponseEntity<>(response, HttpStatus.OK);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, Object>> handleValidationExceptions(MethodArgumentNotValidException ex) {
List<String> msgList = new ArrayList<>();