Skip to content

Commit

Permalink
Fix GroupVersionCondition bug & Refine exception message process
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean committed Jul 30, 2024
1 parent e286565 commit 392b6ac
Show file tree
Hide file tree
Showing 40 changed files with 584 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<Loggers>
<Logger name="org.apache.dubbo.rpc.protocol.tri.stream.StreamUtils" level="error"/>
<Logger name="org.apache.dubbo.rpc.protocol.tri.h12" level="debug"/>
<Logger name="org.apache.dubbo.rpc.protocol.tri.rest" level="debug"/>
<Logger name="org.apache.dubbo.remoting.http12" level="debug"/>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<Logger name="org.apache.dubbo.rpc.protocol.tri.stream.StreamUtils" level="error"/>
<Logger name="org.apache.dubbo.rpc.protocol.tri.h12" level="debug"/>
<Logger name="org.apache.dubbo.rpc.protocol.tri.rest" level="debug"/>
<Logger name="org.apache.dubbo.remoting.http12" level="debug"/>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ public String path() {
return query == null ? request.getRequestURI() : request.getRequestURI() + '?' + query;
}

@Override
public String status() {
return null;
}

@Override
public long id() {
return -1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
*/
package org.apache.dubbo.remoting.http12;

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.JsonUtils;
import org.apache.dubbo.remoting.http12.exception.EncodeException;
import org.apache.dubbo.remoting.http12.exception.HttpStatusException;
import org.apache.dubbo.remoting.http12.message.HttpMessageEncoder;

public abstract class AbstractServerHttpChannelObserver implements CustomizableHttpChannelObserver<Object> {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractServerHttpChannelObserver.class);

private final HttpChannel httpChannel;

private HeadersCustomizer headersCustomizer = HeadersCustomizer.NO_OP;
Expand Down Expand Up @@ -67,9 +72,8 @@ public void setErrorResponseCustomizer(ErrorResponseCustomizer errorResponseCust
}

@Override
@SuppressWarnings("unchecked")
public void setExceptionHandler(ExceptionHandler<?, ?> exceptionHandler) {
this.exceptionHandler = (ExceptionHandler<Throwable, ?>) exceptionHandler;
public void setExceptionHandler(ExceptionHandler<Throwable, ?> exceptionHandler) {
this.exceptionHandler = exceptionHandler;
}

public void setAltSvc(String altSvc) {
Expand Down Expand Up @@ -163,6 +167,9 @@ protected void doOnCompleted(Throwable throwable) {
}
trailersCustomizer.accept(httpMetadata.headers(), throwable);
getHttpChannel().writeHeader(httpMetadata);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Http response trailers sent: {}", httpMetadata.headers());
}
}

protected HttpMetadata encodeTrailers(Throwable throwable) {
Expand Down Expand Up @@ -197,7 +204,7 @@ protected final ErrorResponse buildErrorResponse(String statusCode, Throwable th
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setStatus(statusCode);
if (throwable instanceof HttpStatusException) {
errorResponse.setMessage(throwable.getMessage());
errorResponse.setMessage(((HttpStatusException) throwable).getDisplayMessage());
} else {
errorResponse.setMessage("Internal Server Error");
}
Expand All @@ -214,6 +221,12 @@ protected final HttpOutputMessage buildMessage(Object data) throws Throwable {
data = buildErrorResponse(statusCode, (Throwable) data);
}
}
if (LOGGER.isDebugEnabled()) {
try {
LOGGER.debug("Http response body is: '{}'", JsonUtils.toJson(data));
} catch (Throwable ignored) {
}
}
HttpOutputMessage outputMessage = encodeHttpOutputMessage(data);
try {
preOutputMessage(outputMessage);
Expand Down Expand Up @@ -252,6 +265,9 @@ protected final HttpMetadata buildMetadata(String statusCode, Object data, HttpO
protected final void sendHeader(HttpMetadata httpMetadata) {
getHttpChannel().writeHeader(httpMetadata);
headerSent = true;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Http response headers sent: {}", httpMetadata.headers());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public interface CustomizableHttpChannelObserver<T> extends HttpChannelObserver<

void setErrorResponseCustomizer(ErrorResponseCustomizer errorResponseCustomizer);

void setExceptionHandler(ExceptionHandler<?, ?> exceptionHandler);
void setExceptionHandler(ExceptionHandler<Throwable, ?> exceptionHandler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@

import org.apache.dubbo.common.extension.ExtensionScope;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.common.logger.Level;

@SPI(scope = ExtensionScope.FRAMEWORK)
public interface ExceptionHandler<E extends Throwable, T> {

default Level resolveLogLevel(E error) {
return null;
}

HttpResult<T> handle(E error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.CookieHeaderNames.SameSite;
Expand All @@ -54,6 +55,10 @@ public final class HttpUtils {

private HttpUtils() {}

public static String getStatusMessage(int status) {
return HttpResponseStatus.valueOf(status).reasonPhrase();
}

public static List<HttpCookie> decodeCookies(String value) {
List<HttpCookie> cookies = new ArrayList<>();
for (Cookie c : ServerCookieDecoder.LAX.decodeAll(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

public class DecodeException extends HttpStatusException {

private static final long serialVersionUID = 1L;

public DecodeException() {
super(500);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

public class EncodeException extends HttpStatusException {

private static final long serialVersionUID = 1L;

public EncodeException(String message) {
super(500, message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

public class HttpOverPayloadException extends HttpStatusException {

private static final long serialVersionUID = 1L;

public HttpOverPayloadException(String message) {
super(500, message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

public class HttpRequestTimeout extends HttpStatusException {

private static final long serialVersionUID = 1L;

private final String side;

private HttpRequestTimeout(String side) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
*/
package org.apache.dubbo.remoting.http12.exception;

import org.apache.dubbo.remoting.http12.HttpUtils;

public class HttpStatusException extends RuntimeException {

private static final long serialVersionUID = 1L;

private final int statusCode;

public HttpStatusException(int statusCode) {
this(statusCode, "Unknown Error");
super(HttpUtils.getStatusMessage(statusCode));
this.statusCode = statusCode;
}

public HttpStatusException(int statusCode, String message) {
Expand All @@ -30,7 +35,7 @@ public HttpStatusException(int statusCode, String message) {
}

public HttpStatusException(int statusCode, Throwable cause) {
super(cause);
super(HttpUtils.getStatusMessage(statusCode), cause);
this.statusCode = statusCode;
}

Expand All @@ -42,4 +47,13 @@ public HttpStatusException(int statusCode, String message, Throwable cause) {
public int getStatusCode() {
return statusCode;
}

public String getDisplayMessage() {
return getMessage();
}

@Override
public String toString() {
return getClass().getName() + ": status=" + statusCode + ", " + getMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

public class UnimplementedException extends HttpStatusException {

private static final long serialVersionUID = 1L;

private final String unimplemented;

public UnimplementedException(String unimplemented) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

public class UnsupportedMediaTypeException extends HttpStatusException {

private static final long serialVersionUID = 1L;

private final String mediaType;

public UnsupportedMediaTypeException(String mediaType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.remoting.http12.h1;

import org.apache.dubbo.remoting.http12.HttpHeaderNames;
import org.apache.dubbo.remoting.http12.HttpHeaders;
import org.apache.dubbo.remoting.http12.HttpInputMessage;
import org.apache.dubbo.remoting.http12.RequestMetadata;
Expand Down Expand Up @@ -58,4 +59,10 @@ public String path() {
public void close() throws IOException {
httpInputMessage.close();
}

@Override
public String toString() {
return "Http1Request{method='" + method() + '\'' + ", uri='" + path() + '\'' + ", contentType='"
+ headers().getFirst(HttpHeaderNames.CONTENT_TYPE.getName()) + "'}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

public class CancelStreamException extends RuntimeException implements ErrorCodeHolder {

private final boolean cancelByRemote;
private static final long serialVersionUID = 1L;

private final boolean cancelByRemote;
private final long errorCode;

private CancelStreamException(boolean cancelByRemote, long errorCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ default String path() {
return headers().getFirst(Http2Headers.PATH.getName());
}

default String status() {
return headers().getFirst(Http2Headers.STATUS.getName());
}

@Override
default String name() {
return "HEADER";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.remoting.http12.h2;

import org.apache.dubbo.remoting.http12.HttpHeaderNames;
import org.apache.dubbo.remoting.http12.HttpHeaders;

public class Http2MetadataFrame implements Http2Header {
Expand Down Expand Up @@ -54,4 +55,11 @@ public long id() {
public boolean isEndStream() {
return endStream;
}

@Override
public String toString() {
return "Http2MetadataFrame{method='" + method() + '\'' + ", uri='" + path() + '\'' + ", contentType='"
+ headers().getFirst(HttpHeaderNames.CONTENT_TYPE.getName()) + "', streamId=" + streamId
+ ", endStream=" + endStream + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@

String[] produces() default {};

boolean disabled() default false;
boolean enabled() default true;
}
1 change: 1 addition & 0 deletions dubbo-rpc/dubbo-rpc-triple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-http3</artifactId>
<version>${project.parent.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class TripleProtocol extends AbstractProtocol {
private final RequestMappingRegistry mappingRegistry;
private final TriBuiltinService triBuiltinService;
private final String acceptEncodings;
private boolean http3Bound;

public static boolean CONVERT_NO_LOWER_HEADER = false;
public static boolean IGNORE_1_0_0_VERSION = false;
Expand Down Expand Up @@ -193,6 +194,7 @@ public void afterUnExport() {

if (isHttp3Enabled(url)) {
Http3Exchanger.bind(url);
http3Bound = true;
}

optimizeSerialization(url);
Expand Down Expand Up @@ -232,7 +234,9 @@ public void destroy() {
logger.info("Destroying protocol [" + getClass().getSimpleName() + "] ...");
}
PortUnificationExchanger.close();
Http3Exchanger.close();
if (http3Bound) {
Http3Exchanger.close();
}
pathResolver.destroy();
mappingRegistry.destroy();
super.destroy();
Expand Down
Loading

0 comments on commit 392b6ac

Please sign in to comment.