Skip to content

Commit

Permalink
Merge branch 'master' into fix-5826
Browse files Browse the repository at this point in the history
  • Loading branch information
Aias00 authored Dec 9, 2024
2 parents 2548032 + 8f200ec commit 40bec86
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ public Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPlugin
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;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,8 +100,15 @@ protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPlu
.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
Expand Down Expand Up @@ -270,6 +280,22 @@ private Flux<? extends DataBuffer> appendResponse(final Publisher<? extends Data
});
}

/**
* 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPlu
return WebFluxResultUtils.result(exchange, error);
}
final String domain = upstream.buildDomain();
setDomain(URI.create(domain + shenyuContext.getRealUrl()), exchange);
setDomain(URI.create(domain + (StringUtils.isBlank(ruleHandle.getPath()) ? shenyuContext.getRealUrl() : ruleHandle.getPath())), exchange);
//set time out.
exchange.getAttributes().put(Constants.HTTP_TIME_OUT, ruleHandle.getTimeout());
return chain.execute(exchange);
Expand Down

0 comments on commit 40bec86

Please sign in to comment.