Copilot commented on code in PR #6341:
URL: https://github.com/apache/shenyu/pull/6341#discussion_r3252993815


##########
shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-proxy/src/main/java/org/apache/shenyu/plugin/ai/proxy/enhanced/service/UpstreamErrorLogger.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.ai.proxy.enhanced.service;
+
+import org.slf4j.Logger;
+import 
org.springframework.web.reactive.function.client.WebClientResponseException;
+
+import java.util.Objects;
+
+/**
+ * Shared utility for logging upstream AI service errors with 
WebClientResponseException details.
+ */
+public final class UpstreamErrorLogger {
+
+    private static final int MAX_BODY_LOG_LENGTH = 512;
+
+    private UpstreamErrorLogger() {
+    }
+
+    public static void logUpstreamError(final Logger log, final Throwable e, 
final String mode) {
+        if (Objects.isNull(e)) {
+            return;
+        }
+        final WebClientResponseException webClientEx = 
findWebClientResponseException(e);
+        if (Objects.nonNull(webClientEx)) {
+            log.error("[AiProxy] {} failed, status={}, upstreamBody={}",
+                    mode, webClientEx.getStatusCode(), 
truncateBody(webClientEx.getResponseBodyAsString()), e);
+        } else {
+            log.error("[AiProxy] {} failed", mode, e);
+        }
+    }
+
+    private static String truncateBody(final String body) {
+        if (Objects.isNull(body)) {
+            return "null";
+        }
+        if (body.length() <= MAX_BODY_LOG_LENGTH) {
+            return body;
+        }
+        return body.substring(0, MAX_BODY_LOG_LENGTH) + "...(truncated, total 
" + body.length() + " chars)";

Review Comment:
   The new truncation branch is not covered by the added tests, so a future 
change could regress back to logging full upstream bodies without being caught. 
Add a test with a body longer than MAX_BODY_LOG_LENGTH that verifies the logged 
body is truncated.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to