修改系统内部异常的响应结构体
This commit is contained in:
parent
ba38fbf0cc
commit
9e0729f6f5
|
|
@ -1,17 +1,31 @@
|
|||
package com.lyxh.api.docking.exception;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import java.time.Instant;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
public class ErrorResponse {
|
||||
private final String error;
|
||||
private final String message;
|
||||
private final String timestamp;
|
||||
|
||||
public ErrorResponse(String error, String message) {
|
||||
this.error = error;
|
||||
private final String code;
|
||||
private final String message;
|
||||
private final String responseId;
|
||||
private final DataNode data;
|
||||
|
||||
public ErrorResponse(String code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.timestamp = Instant.now().toString();
|
||||
this.responseId = DigestUtils.md5Hex(UUID.randomUUID().toString());
|
||||
this.data = new DataNode();
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class DataNode {
|
||||
private final int status = 0;
|
||||
|
||||
@JsonProperty("result")
|
||||
private final Object result = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ public class GlobalExceptionHandler {
|
|||
.reduce((a, b) -> a + "; " + b)
|
||||
.orElse("validation failed");
|
||||
return ResponseEntity.badRequest()
|
||||
.body(new ErrorResponse("VALIDATION_ERROR", msg));
|
||||
.body(new ErrorResponse("400", msg));
|
||||
}
|
||||
|
||||
@ExceptionHandler(ApiCallException.class)
|
||||
|
|
@ -34,13 +34,13 @@ public class GlobalExceptionHandler {
|
|||
String detail = ex.getResponseBody() != null && !ex.getResponseBody().isEmpty()
|
||||
? ex.getResponseBody() : ex.getMessage();
|
||||
return ResponseEntity.status(status)
|
||||
.body(new ErrorResponse(ex.getCode(), detail));
|
||||
.body(new ErrorResponse(String.valueOf(status.value()), detail));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleGeneral(Exception ex) {
|
||||
log.error("Unexpected error", ex);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(new ErrorResponse("INTERNAL_ERROR", "Internal server error"));
|
||||
.body(new ErrorResponse("500", "Internal server error"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
24
对接接口文档.md
24
对接接口文档.md
|
|
@ -105,22 +105,26 @@
|
|||
|
||||
## 错误响应
|
||||
|
||||
统一错误格式:
|
||||
错误响应与三方接口响应保持相同结构,其中`data` 中 `status=0`、`result=null`:
|
||||
|
||||
```json
|
||||
{
|
||||
"error": "ERROR_CODE",
|
||||
"message": "错误描述",
|
||||
"timestamp": "2026-07-08T10:30:00Z"
|
||||
"code": "503",
|
||||
"message": "External API unreachable after 1 attempts",
|
||||
"responseId": "a1b2c3d4e5f6789012345678abcdef01",
|
||||
"data": {
|
||||
"status": 0,
|
||||
"result": null
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
| HTTP状态码 | error | 说明 |
|
||||
|---|---|------------------|
|
||||
| 400 | VALIDATION_ERROR | 请求参数校验失败 |
|
||||
| 502 | HTTP_ERROR | 三方 API HTTP 错误 |
|
||||
| 503 | NETWORK_ERROR | 三方 API 网络不可达 |
|
||||
| 500 | INTERNAL_ERROR | 服务内部未知异常 api配置信息未找到 |
|
||||
| HTTP状态码 | code | 说明 |
|
||||
|---|---|---|
|
||||
| 400 | 400 | 请求参数校验失败 |
|
||||
| 502 | 502 | 三方 API HTTP 错误 |
|
||||
| 503 | 503 | 三方 API 网络不可达 |
|
||||
| 500 | 500 | 服务内部未知异常 |
|
||||
|
||||
## 完整调用示例
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue