This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new e8525767ef fix: handle non-standard HTTP status codes in logging
response and add test coverage (#6918)
e8525767ef is described below
commit e8525767efbc68eb5436def1c27b46c64df5e586
Author: Limbo <[email protected]>
AuthorDate: Mon Aug 17 10:20:37 2026 +0800
fix: handle non-standard HTTP status codes in logging response and add test
coverage (#6918)
Co-authored-by: aias00 <[email protected]>
---
.../common/body/LoggingServerHttpResponse.java | 11 ++-
.../body/LoggingServerHttpResponseStatusTest.java | 105 +++++++++++++++++++++
2 files changed, 113 insertions(+), 3 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponse.java
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponse.java
index 43a733ec45..8c488727ee 100644
---
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponse.java
+++
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponse.java
@@ -234,11 +234,16 @@ public class LoggingServerHttpResponse<L extends
ShenyuRequestLog> extends Serve
if (throwable instanceof ResponseStatusException) {
httpStatus = ((ResponseStatusException) throwable).getStatusCode();
}
- logInfo.setStatus(httpStatus.value());
+ final int statusCode = httpStatus.value();
+ logInfo.setStatus(statusCode);
logInfo.setTraceId(getTraceId());
+ final HttpStatus resolvedStatus = HttpStatus.resolve(statusCode);
+ final String reasonPhrase = Objects.isNull(resolvedStatus)
+ ? String.valueOf(statusCode)
+ : resolvedStatus.getReasonPhrase();
// Do not collect stack
- Object result = ShenyuResultWrap.error(exchange, httpStatus.value(),
- ((HttpStatus) httpStatus).getReasonPhrase(),
throwable.getMessage());
+ Object result = ShenyuResultWrap.error(exchange, statusCode,
+ reasonPhrase, throwable.getMessage());
final ShenyuResult<?> shenyuResult = ShenyuResultWrap.shenyuResult();
Object resultData = shenyuResult.format(exchange, result);
final Object responseData = shenyuResult.result(exchange, resultData);
diff --git
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/test/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponseStatusTest.java
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/test/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponseStatusTest.java
new file mode 100644
index 0000000000..26dd9f48be
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/test/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponseStatusTest.java
@@ -0,0 +1,105 @@
+/*
+ * 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.logging.common.body;
+
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.apache.shenyu.plugin.api.result.DefaultShenyuResult;
+import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
+import org.apache.shenyu.plugin.logging.common.collector.LogCollector;
+import org.apache.shenyu.plugin.logging.common.entity.ShenyuRequestLog;
+import org.apache.shenyu.plugin.logging.desensitize.api.matcher.KeyWordMatch;
+import org.junit.jupiter.api.Test;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.http.HttpStatusCode;
+import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
+import org.springframework.mock.web.server.MockServerWebExchange;
+import org.springframework.web.server.ResponseStatusException;
+import org.springframework.web.server.ServerWebExchange;
+
+import java.time.LocalDateTime;
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Test cases for status handling in {@link LoggingServerHttpResponse}.
+ */
+public final class LoggingServerHttpResponseStatusTest {
+
+ @Test
+ public void testLogErrorWithNonStandardStatusCode() {
+ ApplicationContext previousContext =
SpringBeanUtils.getInstance().getApplicationContext();
+ try (GenericApplicationContext applicationContext = new
GenericApplicationContext()) {
+ applicationContext.registerBean(DefaultShenyuResult.class);
+ applicationContext.refresh();
+
SpringBeanUtils.getInstance().setApplicationContext(applicationContext);
+
+ ServerWebExchange exchange =
MockServerWebExchange.from(MockServerHttpRequest.get("/").build());
+ ShenyuContext shenyuContext = new ShenyuContext();
+ shenyuContext.setStartDateTime(LocalDateTime.now());
+ exchange.getAttributes().put(Constants.CONTEXT, shenyuContext);
+
+ AtomicReference<ShenyuRequestLog> collectedLog = new
AtomicReference<>();
+ LogCollector<ShenyuRequestLog> logCollector = new
RecordingLogCollector(collectedLog);
+ LoggingServerHttpResponse<ShenyuRequestLog> response = new
LoggingServerHttpResponse<>(exchange.getResponse(),
+ new ShenyuRequestLog(), logCollector, false,
Collections.emptySet(), "dataMaskByCharReplace");
+ response.setExchange(exchange);
+
+ response.logError(new
ResponseStatusException(HttpStatusCode.valueOf(599), "error"));
+
+ ShenyuRequestLog requestLog = collectedLog.get();
+ assertNotNull(requestLog);
+ assertEquals(599, requestLog.getStatus());
+
assertTrue(requestLog.getResponseBody().contains("\"message\":\"599\""));
+ } finally {
+
SpringBeanUtils.getInstance().setApplicationContext(previousContext);
+ }
+ }
+
+ private static final class RecordingLogCollector implements
LogCollector<ShenyuRequestLog> {
+
+ private final AtomicReference<ShenyuRequestLog> collectedLog;
+
+ private RecordingLogCollector(final AtomicReference<ShenyuRequestLog>
collectedLog) {
+ this.collectedLog = collectedLog;
+ }
+
+ @Override
+ public void start() {
+ }
+
+ @Override
+ public void desensitize(final ShenyuRequestLog log, final KeyWordMatch
keyWordMatch,
+ final String desensitizeAlg) {
+ }
+
+ @Override
+ public void collect(final ShenyuRequestLog log) {
+ collectedLog.set(log);
+ }
+
+ @Override
+ public void close() {
+ }
+ }
+}