mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 12:52:07 +08:00
增加组件排单接口调用日志
This commit is contained in:
+53
-27
@@ -45,8 +45,11 @@ public class ProducingPlanOrderClient {
|
||||
blockIds, workflowSchemeId, customMaterialGroupList);
|
||||
log.info("Submitting producing plan order customMaterialGroupList={}",
|
||||
serialize(customMaterialGroupList));
|
||||
String method = "POST";
|
||||
String url = properties.getBaseUrl();
|
||||
String requestBody = serialize(request);
|
||||
HttpResponse<String> response = send(
|
||||
properties.getBaseUrl(), token, "POST", serialize(request), "create");
|
||||
url, token, method, requestBody, "create");
|
||||
try {
|
||||
JsonNode result = objectMapper.readTree(response.body());
|
||||
JsonNode planOrderIdNode = result;
|
||||
@@ -54,44 +57,48 @@ public class ProducingPlanOrderClient {
|
||||
JsonNode code = result.get("code");
|
||||
if (code == null || !code.isIntegralNumber() || code.intValue() != 0) {
|
||||
String message = result.path("message").asText("business error");
|
||||
log.error("Producing plan order create failed, responseBody={}",
|
||||
responseBodyForLog(response.body()));
|
||||
logRequestFailure("create", method, url, requestBody,
|
||||
response.statusCode(), response.body(), null);
|
||||
throw syncError("create: " + message);
|
||||
}
|
||||
planOrderIdNode = result.get("data");
|
||||
}
|
||||
if (planOrderIdNode == null
|
||||
|| (!planOrderIdNode.isIntegralNumber() && !planOrderIdNode.isTextual())) {
|
||||
logRequestFailure("create", method, url, requestBody,
|
||||
response.statusCode(), response.body(), null);
|
||||
throw syncError("create: invalid plan order id");
|
||||
}
|
||||
return planOrderIdNode.isIntegralNumber()
|
||||
? planOrderIdNode.longValue() : Long.valueOf(planOrderIdNode.textValue());
|
||||
} catch (JsonProcessingException | NumberFormatException ex) {
|
||||
log.error("Producing plan order create response cannot be parsed, responseBody={}",
|
||||
responseBodyForLog(response.body()), ex);
|
||||
logRequestFailure("create", method, url, requestBody,
|
||||
response.statusCode(), response.body(), ex);
|
||||
throw syncError("create: invalid plan order id");
|
||||
}
|
||||
}
|
||||
|
||||
public void appendBlocks(String token, Long planOrderId, List<String> blockIds) {
|
||||
HttpResponse<String> response = send(
|
||||
resourceUrl(planOrderId, "append-block-id"), token, "POST",
|
||||
serialize(new AppendRequest(blockIds)), "append-block-id");
|
||||
validateBusinessResponse(response, "append-block-id");
|
||||
String method = "POST";
|
||||
String url = resourceUrl(planOrderId, "append-block-id");
|
||||
String requestBody = serialize(new AppendRequest(blockIds));
|
||||
HttpResponse<String> response = send(url, token, method, requestBody, "append-block-id");
|
||||
validateBusinessResponse(response, "append-block-id", method, url, requestBody);
|
||||
}
|
||||
|
||||
public void deleteBlocks(String token, Long planOrderId, List<String> blockIds) {
|
||||
HttpResponse<String> response = send(
|
||||
resourceUrl(planOrderId, "delete-block-id"), token, "DELETE",
|
||||
serialize(new DeleteBlocksRequest(blockIds)), "delete-block-id");
|
||||
validateBusinessResponse(response, "delete-block-id");
|
||||
String method = "DELETE";
|
||||
String url = resourceUrl(planOrderId, "delete-block-id");
|
||||
String requestBody = serialize(new DeleteBlocksRequest(blockIds));
|
||||
HttpResponse<String> response = send(url, token, method, requestBody, "delete-block-id");
|
||||
validateBusinessResponse(response, "delete-block-id", method, url, requestBody);
|
||||
}
|
||||
|
||||
public void delete(String token, Long planOrderId) {
|
||||
HttpResponse<String> response = send(
|
||||
properties.getBaseUrl() + "/" + planOrderId, token, "DELETE",
|
||||
null, "delete");
|
||||
validateBusinessResponse(response, "delete");
|
||||
String method = "DELETE";
|
||||
String url = properties.getBaseUrl() + "/" + planOrderId;
|
||||
HttpResponse<String> response = send(url, token, method, null, "delete");
|
||||
validateBusinessResponse(response, "delete", method, url, null);
|
||||
}
|
||||
|
||||
private String resourceUrl(Long planOrderId, String action) {
|
||||
@@ -122,18 +129,17 @@ public class ProducingPlanOrderClient {
|
||||
HttpResponse<String> response = httpClient.send(
|
||||
request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
log.error("Producing plan order operation {} failed, url={}, httpStatus={}, "
|
||||
+ "responseBody={}",
|
||||
operation, url, response.statusCode(),
|
||||
responseBodyForLog(response.body()));
|
||||
logRequestFailure(operation, method, url, body,
|
||||
response.statusCode(), response.body(), null);
|
||||
throw syncError(operation + ": HTTP " + response.statusCode());
|
||||
}
|
||||
return response;
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
logRequestFailure(operation, method, url, body, null, null, ex);
|
||||
throw syncError(operation + ": request interrupted");
|
||||
} catch (IOException ex) {
|
||||
log.warn("Producing plan order operation {} failed", operation, ex);
|
||||
logRequestFailure(operation, method, url, body, null, null, ex);
|
||||
throw syncError(operation + ": " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -142,7 +148,8 @@ public class ProducingPlanOrderClient {
|
||||
* 对方写接口可能以 HTTP 200 返回统一业务响应,此处补充业务码校验。
|
||||
* 空响应或未包含 code 的响应仍按接口文档视为成功。
|
||||
*/
|
||||
private void validateBusinessResponse(HttpResponse<String> response, String operation) {
|
||||
private void validateBusinessResponse(HttpResponse<String> response, String operation,
|
||||
String method, String url, String requestBody) {
|
||||
if (response.body() == null || response.body().isBlank()) {
|
||||
return;
|
||||
}
|
||||
@@ -151,17 +158,36 @@ public class ProducingPlanOrderClient {
|
||||
JsonNode code = result == null || !result.isObject() ? null : result.get("code");
|
||||
if (code != null && code.isIntegralNumber() && code.intValue() != 0) {
|
||||
String message = result.path("message").asText("business error");
|
||||
log.error("Producing plan order operation {} failed, responseBody={}",
|
||||
operation, responseBodyForLog(response.body()));
|
||||
logRequestFailure(operation, method, url, requestBody,
|
||||
response.statusCode(), response.body(), null);
|
||||
throw syncError(operation + ": " + message);
|
||||
}
|
||||
} catch (JsonProcessingException ex) {
|
||||
log.warn("Producing plan order operation {} returned a non-JSON success response, "
|
||||
+ "responseBody={}",
|
||||
operation, responseBodyForLog(response.body()));
|
||||
+ "method={}, url={}, requestBody={}, responseBody={}",
|
||||
operation, method, url, requestBody,
|
||||
responseBodyForLog(response.body()), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void logRequestFailure(String operation, String method, String url,
|
||||
String requestBody, Integer httpStatus,
|
||||
String responseBody, Throwable ex) {
|
||||
String message = "Producing plan order operation {} failed, method={}, url={}, "
|
||||
+ "requestBody={}, httpStatus={}, responseBody={}";
|
||||
if (ex == null) {
|
||||
log.error(message, operation, method, url,
|
||||
requestBody == null ? "" : requestBody,
|
||||
httpStatus == null ? "" : httpStatus,
|
||||
responseBodyForLog(responseBody));
|
||||
return;
|
||||
}
|
||||
log.error(message, operation, method, url,
|
||||
requestBody == null ? "" : requestBody,
|
||||
httpStatus == null ? "" : httpStatus,
|
||||
responseBodyForLog(responseBody), ex);
|
||||
}
|
||||
|
||||
private String responseBodyForLog(String body) {
|
||||
if (body == null) {
|
||||
return "";
|
||||
|
||||
Reference in New Issue
Block a user