eye-gu commented on code in PR #6341: URL: https://github.com/apache/shenyu/pull/6341#discussion_r3363279655
########## 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,74 @@ +/* + * 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"; Review Comment: This method is used to prevent content from being too long and truncated during printing. The verification here is defensive programming, and if the input parameter is null, it is also reasonable to print null Is this statement correct -- 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]
