refactor(api): 统一 HTTP 200 响应契约
This commit is contained in:
@@ -6,8 +6,6 @@ import com.baiyue.common.core.exception.BizException;
|
||||
import com.baiyue.common.security.AuthenticatedUser;
|
||||
import com.baiyue.file.client.IamQualificationClient;
|
||||
import com.baiyue.file.service.LocalQualificationStorage;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import feign.FeignException;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -26,7 +24,6 @@ public class QualificationFileController {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(QualificationFileController.class);
|
||||
@Resource private LocalQualificationStorage storage;
|
||||
@Resource private IamQualificationClient iamClient;
|
||||
@Resource private ObjectMapper objectMapper;
|
||||
@Value("${baiyue.security.internal-service-token}") private String internalToken;
|
||||
|
||||
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@@ -41,7 +38,16 @@ public class QualificationFileController {
|
||||
var response = iamClient.submit(internalToken,
|
||||
new IamQualificationClient.QualificationRequest(tenantId, "BUSINESS_LICENSE",
|
||||
certificateNo, stored.documentUrl()));
|
||||
if (response == null || !response.successful() || response.data() == null) {
|
||||
if (response == null) {
|
||||
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "Qualification workflow failed");
|
||||
}
|
||||
if (!response.successful()) {
|
||||
ResultCode code = ResultCode.of(response.code());
|
||||
String message = response.message() == null || response.message().isBlank()
|
||||
? code.message() : response.message();
|
||||
throw new BizException(code, message);
|
||||
}
|
||||
if (response.data() == null) {
|
||||
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "Qualification workflow failed");
|
||||
}
|
||||
var data = response.data();
|
||||
@@ -50,35 +56,11 @@ public class QualificationFileController {
|
||||
} catch (RuntimeException exception) {
|
||||
storage.delete(stored);
|
||||
if (exception instanceof BizException business) throw business;
|
||||
if (exception instanceof FeignException feign) {
|
||||
LOGGER.warn("IAM 资质登记被拒绝:status={} body={}", feign.status(), feign.contentUTF8());
|
||||
if (feign.status() >= 400 && feign.status() < 500) {
|
||||
throw new BizException(resultCodeOf(feign.status()), iamMessage(feign));
|
||||
}
|
||||
}
|
||||
LOGGER.error("IAM 资质登记调用失败", exception);
|
||||
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "IAM qualification service is unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
/** 透出 IAM 返回的业务错误消息,避免把"已审核通过""重复提交"等业务拒绝误报为服务不可用。 */
|
||||
private String iamMessage(FeignException feign) {
|
||||
try {
|
||||
String message = objectMapper.readTree(feign.contentUTF8()).path("message").asText(null);
|
||||
if (message != null && !message.isBlank()) return message;
|
||||
} catch (Exception ignored) { }
|
||||
return "Enterprise qualification submission was rejected";
|
||||
}
|
||||
|
||||
private ResultCode resultCodeOf(int status) {
|
||||
return switch (status) {
|
||||
case 403 -> ResultCode.FORBIDDEN;
|
||||
case 404 -> ResultCode.NOT_FOUND;
|
||||
case 409 -> ResultCode.CONFLICT;
|
||||
default -> ResultCode.BUSINESS_ERROR;
|
||||
};
|
||||
}
|
||||
|
||||
@GetMapping("/{fileName}")
|
||||
public ResponseEntity<org.springframework.core.io.Resource> download(
|
||||
@AuthenticationPrincipal AuthenticatedUser user, @PathVariable String fileName) {
|
||||
|
||||
Reference in New Issue
Block a user