20260831172912
This commit is contained in:
1 parent
d6a56eb17f
commit
4d46ce6c53
61 files changed
+2086
-2979
No files matched your search
+9
-1
@@ -42,7 +42,15 @@ public class GlobalExceptionHandler {
|
||||
details.put("table", exception.getTable());
|
||||
details.put("action", exception.getAction());
|
||||
details.put("index", exception.getIndex());
|
||||
return new ApiResponse<>(ApiResponse.SAVE_ERROR_CODE, "保存失败", details);
|
||||
String rootCause = exception.rootCauseMessage();
|
||||
if (rootCause != null) {
|
||||
details.put("cause", rootCause);
|
||||
}
|
||||
return new ApiResponse<>(
|
||||
ApiResponse.SAVE_ERROR_CODE,
|
||||
rootCause == null ? "保存失败" : "保存失败: " + rootCause,
|
||||
details
|
||||
);
|
||||
}
|
||||
|
||||
@ExceptionHandler({SQLException.class, DataAccessException.class})
|
||||
|
||||
@@ -29,4 +29,16 @@ public class SaveObjectException extends RuntimeException {
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根因摘要(如 SQLServerException 的 "Violation of UNIQUE constraint ..."),
|
||||
* 用于接口响应中提示具体失败原因,避免只看到笼统的「保存失败」。
|
||||
*/
|
||||
public String rootCauseMessage() {
|
||||
Throwable root = getCause();
|
||||
while (root != null && root.getCause() != null) {
|
||||
root = root.getCause();
|
||||
}
|
||||
return root == null ? null : root.getMessage();
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -39,9 +39,10 @@ class GlobalExceptionHandlerTests {
|
||||
ApiResponse<Map<String, Object>> response = handler.handleSaveObjectException(exception);
|
||||
|
||||
assertThat(response.code()).isEqualTo(ApiResponse.SAVE_ERROR_CODE);
|
||||
assertThat(response.message()).isEqualTo("保存失败");
|
||||
assertThat(response.message()).isEqualTo("保存失败: update failed");
|
||||
assertThat(response.data()).containsEntry("table", "bs_business");
|
||||
assertThat(response.data()).containsEntry("action", "update");
|
||||
assertThat(response.data()).containsEntry("index", 2);
|
||||
assertThat(response.data()).containsEntry("cause", "update failed");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user