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