[type:fix] fix LoggingPlugin error log catch .
diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/AbstractLoggingPlugin.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/AbstractLoggingPlugin.java
index dca4485..0374f1a 100644
--- a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/AbstractLoggingPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/AbstractLoggingPlugin.java
@@ -119,7 +119,12 @@
ServerWebExchange webExchange = exchange.mutate().request(loggingServerHttpRequest)
.response(loggingServerHttpResponse).build();
loggingServerHttpResponse.setExchange(webExchange);
- return chain.execute(webExchange).doOnError(loggingServerHttpResponse::logError);
+ try {
+ return chain.execute(webExchange).doOnError(loggingServerHttpResponse::logError);
+ } catch (Exception e) {
+ loggingServerHttpResponse.logError(e);
+ throw e;
+ }
}
/**
diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java
index b315f2b..9352d3f 100644
--- a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java
@@ -38,12 +38,15 @@
import org.slf4j.LoggerFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.util.MultiValueMap;
+import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -97,8 +100,15 @@
.append(getRequestMethod(request, desensitized, keyWordMatch)).append(System.lineSeparator())
.append(getRequestHeaders(request, desensitized, keyWordMatch)).append(System.lineSeparator())
.append(getQueryParams(request, desensitized, keyWordMatch)).append(System.lineSeparator());
- return chain.execute(exchange.mutate().request(new LoggingServerHttpRequest(request, requestInfo, desensitized, keyWordMatch))
- .response(new LoggingServerHttpResponse(exchange.getResponse(), requestInfo, desensitized, keyWordMatch)).build());
+ final LoggingServerHttpResponse loggingServerHttpResponse = new LoggingServerHttpResponse(exchange.getResponse(), requestInfo, desensitized, keyWordMatch);
+ try {
+ return chain.execute(exchange.mutate().request(new LoggingServerHttpRequest(request, requestInfo, desensitized, keyWordMatch))
+ .response(loggingServerHttpResponse).build())
+ .doOnError(loggingServerHttpResponse::logError);
+ } catch (Exception e) {
+ loggingServerHttpResponse.logError(e);
+ throw e;
+ }
}
@Override
@@ -270,6 +280,22 @@
});
}
+ /**
+ * access error.
+ *
+ * @param throwable Exception occurred。
+ */
+ public void logError(final Throwable throwable) {
+ HttpStatusCode httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
+ if (throwable instanceof ResponseStatusException) {
+ httpStatus = ((ResponseStatusException) throwable).getStatusCode();
+ }
+ logInfo.append("Response Code: ").append(httpStatus).append(System.lineSeparator());
+ logInfo.append(getResponseHeaders()).append(System.lineSeparator());
+ logInfo.append("ERROR: ").append(System.lineSeparator());
+ logInfo.append(throwable.getMessage()).append(System.lineSeparator());
+ }
+
private String getResponseHeaders() {
return System.lineSeparator() + "[Response Headers Start]" + System.lineSeparator()
+ LoggingConsolePlugin.this.getHeaders(serverHttpResponse.getHeaders(), desensitized, keyWordMatch)