Skip to content

Commit

Permalink
Add error log for OpenAPI processing
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean committed Nov 25, 2024
1 parent 5608d08 commit 9753552
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
package org.apache.dubbo.rpc.protocol.tri;

import org.apache.dubbo.common.logger.Level;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.remoting.http12.HttpStatus;
import org.apache.dubbo.remoting.http12.exception.HttpStatusException;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -30,8 +33,6 @@

public class ExceptionUtils {

private static final int NOT_FOUND = -1;

public static String getStackTrace(final Throwable throwable) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
Expand Down Expand Up @@ -82,6 +83,19 @@ public static List<String> getStackFrameList(final Throwable t) {
return getStackFrameList(t, Integer.MAX_VALUE);
}

public static Level resolveLogLevel(final Throwable t) {
if (t instanceof HttpStatusException) {
int httpStatusCode = ((HttpStatusException) t).getStatusCode();
if (httpStatusCode < HttpStatus.BAD_REQUEST.getCode()) {
return TripleProtocol.VERBOSE_ENABLED ? Level.INFO : Level.DEBUG;
}
if (httpStatusCode < HttpStatus.INTERNAL_SERVER_ERROR.getCode()) {
return Level.INFO;
}
}
return Level.ERROR;
}

/**
* Wrap as runtime exception
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,7 @@ public Level resolveLogLevel(Throwable throwable) {
}
}

if (throwable instanceof HttpStatusException) {
int httpStatusCode = ((HttpStatusException) throwable).getStatusCode();
if (httpStatusCode < HttpStatus.BAD_REQUEST.getCode()) {
return TripleProtocol.VERBOSE_ENABLED ? Level.INFO : Level.DEBUG;
}
if (httpStatusCode < HttpStatus.INTERNAL_SERVER_ERROR.getCode()) {
return Level.INFO;
}
}
return Level.ERROR;
return ExceptionUtils.resolveLogLevel(throwable);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.rpc.protocol.tri.rest.openapi;

import org.apache.dubbo.common.logger.FluentLogger;
import org.apache.dubbo.common.logger.Level;
import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
import org.apache.dubbo.common.utils.LRUCache;
import org.apache.dubbo.common.utils.Pair;
Expand All @@ -30,6 +31,7 @@
import org.apache.dubbo.remoting.http12.rest.OpenAPIRequest;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.tri.ExceptionUtils;
import org.apache.dubbo.rpc.protocol.tri.rest.RestConstants;
import org.apache.dubbo.rpc.protocol.tri.rest.mapping.RadixTree;
import org.apache.dubbo.rpc.protocol.tri.rest.mapping.RadixTree.Match;
Expand Down Expand Up @@ -173,34 +175,42 @@ private List<OpenAPI> resolveOpenAPIs() {

@Override
public String getDocument(OpenAPIRequest request) {
request = Helper.formatRequest(request);
String path = null;
HttpResult<?> result;
try {
request = Helper.formatRequest(request);

HttpRequest httpRequest = RpcContext.getServiceContext().getRequest(HttpRequest.class);
if (!RequestUtils.isRestRequest(httpRequest)) {
return handleDocument(request);
}
HttpRequest httpRequest = RpcContext.getServiceContext().getRequest(HttpRequest.class);
if (!RequestUtils.isRestRequest(httpRequest)) {
return handleDocument(request);
}

String path = RequestUtils.getPathVariable(httpRequest, "path");
if (StringUtils.isEmpty(path)) {
throw HttpResult.found(PathUtils.join(httpRequest.path(), "swagger-ui/index.html"))
.toPayload();
}
path = RequestUtils.getPathVariable(httpRequest, "path");
if (StringUtils.isEmpty(path)) {
throw HttpResult.found(PathUtils.join(httpRequest.path(), "swagger-ui/index.html")).toPayload();
}

path = '/' + path;
List<Match<OpenAPIRequestHandler>> matches = tree.matchRelaxed(path);
if (matches.isEmpty()) {
throw new HttpStatusException(HttpStatus.NOT_FOUND.getCode());
}
path = '/' + path;
List<Match<OpenAPIRequestHandler>> matches = tree.matchRelaxed(path);
if (matches.isEmpty()) {
throw new HttpStatusException(HttpStatus.NOT_FOUND.getCode());
}

Collections.sort(matches);
Match<OpenAPIRequestHandler> match = matches.get(0);
HttpResponse httpResponse = RpcContext.getServiceContext().getResponse(HttpResponse.class);
if (request.getFormat() == null) {
request.setFormat(Helper.parseFormat(httpResponse.contentType()));
Collections.sort(matches);
Match<OpenAPIRequestHandler> match = matches.get(0);
HttpResponse httpResponse = RpcContext.getServiceContext().getResponse(HttpResponse.class);
if (request.getFormat() == null) {
request.setFormat(Helper.parseFormat(httpResponse.contentType()));
}
httpRequest.setAttribute(OpenAPIRequest.class.getName(), request);
httpRequest.setAttribute(RestConstants.URI_TEMPLATE_VARIABLES_ATTRIBUTE, match.getVariableMap());
result = match.getValue().handle(path, httpRequest, httpResponse);
} catch (Throwable t) {
Level level = ExceptionUtils.resolveLogLevel(t);
LOG.log(level, "Failed to processing OpenAPI request {} for path: '{}'", request, path, t);
throw t;
}
httpRequest.setAttribute(OpenAPIRequest.class.getName(), request);
httpRequest.setAttribute(RestConstants.URI_TEMPLATE_VARIABLES_ATTRIBUTE, match.getVariableMap());
throw match.getValue().handle(path, httpRequest, httpResponse).toPayload();
throw result.toPayload();
}

private String handleDocument(OpenAPIRequest request) {
Expand Down

0 comments on commit 9753552

Please sign in to comment.