F2-8: 写链路异常友好响应 + 附件控件映射

- 新增 WriteExceptionHandler:ValidationException→422、PrivilegeException→403、
  RuntimeException→500,均返回 amis 约定 {status,msg},不再裸透 Spring 500
- UiTypeMapper 新增 MultiFileUploadField/FileUploadField → input-file
  (receiver=/api/file/upload, downloadUrl=/api/file/${id}, multiple)
- 转换器单测新增 form_附件字段MultiFileUploadField转inputFile(28/28 pass)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-12 00:47:31 +08:00
parent 2d8a16eef6
commit e876327c75
3 changed files with 135 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
package com.eip4.platform.metadata.web;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.dsasp.server.common.exception.PrivilegeException;
import com.dsasp.server.common.exception.ValidationException;
/**
* F2-8 写链路异常 → amis 友好响应。
*
* <p>拦截 EntityWriteController / FileController 抛出的业务异常,
* 转换成 amis 约定格式,避免 Spring 默认 500 裸 JSON 透传前端。
*
* <ul>
* <li>{@link ValidationException} — 业务校验失败(必填/重复/约束等)→
* HTTP 422 + {@code {status:422, msg}} (amis form 默认对 status≠0 显示顶部错误提示)</li>
* <li>{@link PrivilegeException} — 数据权限拒绝 →
* HTTP 403 + {@code {status:403, msg}}</li>
* </ul>
*
* <p>纪律:零改 {@code com.dsasp};不依赖 Spring Validation(引擎校验经 ValidationException 抛出)。
*/
@RestControllerAdvice(assignableTypes = {EntityWriteController.class, FileController.class})
public class WriteExceptionHandler {
/**
* 业务校验失败(引擎 ValidationException)→ HTTP 422 amis 格式。
*
* <p>amis form 约定:{@code status≠0} 时显示顶部错误提示({@code msg} 字段);
* 若需字段级行内错误则需 {@code status:422, errors:{fieldName:msg}},
* 但引擎 ValidationException 消息不含字段名(如"分类是必填项"),
* 故只返回 msg,amis form 会在顶部 toast/alert 区显示。
*/
@ExceptionHandler(ValidationException.class)
public ResponseEntity<Map<String, Object>> handleValidation(ValidationException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("status", 422);
body.put("msg", ex.getMessage() != null ? ex.getMessage() : "数据校验失败,请检查输入");
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(body);
}
/**
* 数据权限拒绝(写侧 PrivilegeException)→ HTTP 403 amis 格式。
*
* <p>amis form 收到 403 时默认显示 msg 文案;前端不再裸显 Spring 默认 403 JSON。
*/
@ExceptionHandler(PrivilegeException.class)
public ResponseEntity<Map<String, Object>> handlePrivilege(PrivilegeException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("status", 403);
body.put("msg", ex.getMessage() != null ? ex.getMessage() : "无操作权限");
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(body);
}
/**
* 兜底:其他 RuntimeException(引擎 SQL/状态机异常等)→ HTTP 500 amis 格式。
*
* <p>把裸 Spring 500 JSON 替换为 amis 约定体,前端 amis form 可显示 msg 而非空白。
* 注意仅对 EntityWriteController / FileController 生效(assignableTypes 限定),
* 不干扰其他 Controller 的异常处理链。
*/
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<Map<String, Object>> handleRuntime(RuntimeException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("status", 1);
// 暴露简短消息(不含堆栈);内部引擎消息通常已是中文业务描述
String msg = ex.getMessage();
if (msg == null || msg.isBlank()) {
msg = "服务器内部错误,请稍后重试";
} else if (msg.length() > 200) {
// 截断过长的技术消息,防止泄露内部细节
msg = msg.substring(0, 200) + "…";
}
body.put("msg", msg);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(body);
}
}