Skip to content

Commit 499c513

Browse files
committed
Update exception
1 parent 5e24e3d commit 499c513

File tree

6 files changed

+59
-39
lines changed

6 files changed

+59
-39
lines changed

joylive-plugin/joylive-router/joylive-router-springweb5/src/main/java/com/jd/live/agent/plugin/router/springweb/v5/interceptor/DispatcherHandlerInterceptor.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,13 @@
3535

3636
import java.util.function.Function;
3737

38-
import static com.jd.live.agent.bootstrap.util.type.FieldAccessorFactory.getQuietly;
3938
import static com.jd.live.agent.governance.util.ResponseUtils.labelHeaders;
4039

4140
/**
4241
* DispatcherHandlerInterceptor
4342
*/
4443
public class DispatcherHandlerInterceptor extends InterceptorAdaptor {
4544

46-
private static final String FIELD_EXCEPTION_HANDLER = "exceptionHandler";
47-
4845
private final InvocationContext context;
4946

5047
private final JsonPathParser parser;
@@ -62,8 +59,8 @@ public void onEnter(ExecutableContext ctx) {
6259
McpConfig mcpConfig = govnConfig.getMcpConfig();
6360
ServiceConfig serviceConfig = govnConfig.getServiceConfig();
6461
MethodContext mc = (MethodContext) ctx;
65-
ServerWebExchange exchange = (ServerWebExchange) mc.getArguments()[0];
66-
Object handler = mc.getArguments()[1];
62+
ServerWebExchange exchange = mc.getArgument(0);
63+
Object handler = mc.getArgument(1);
6764
ReactiveInboundRequest request = new ReactiveInboundRequest(exchange.getRequest(), handler, serviceConfig::isSystem, mcpConfig::isMcp, parser);
6865
if (!request.isSystem()) {
6966
InboundInvocation<ReactiveInboundRequest> invocation = context.getApplication().getService().isGateway()
@@ -77,7 +74,7 @@ public void onEnter(ExecutableContext ctx) {
7774
labelHeaders(ex, headers::set);
7875
}).doOnSuccess(result -> {
7976
if (result != null) {
80-
Function<Throwable, Mono<HandlerResult>> exceptionHandler = getQuietly(result, FIELD_EXCEPTION_HANDLER);
77+
Function<Throwable, Mono<HandlerResult>> exceptionHandler = CloudUtils.getExceptionHandler(result);
8178
result.setExceptionHandler(ex -> {
8279
HttpHeaders headers = CloudUtils.writable(exchange.getResponse().getHeaders());
8380
labelHeaders(ex, headers::set);

joylive-plugin/joylive-router/joylive-router-springweb5/src/main/java/com/jd/live/agent/plugin/router/springweb/v5/util/CloudUtils.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public class CloudUtils {
3737

3838
private static final FieldAccessor ACCESSOR_HANDLER = getAccessor(CLASS_HANDLER_METHOD, "bean");
3939

40+
private static final String TYPE_HANDLER_RESULT = "org.springframework.web.reactive.HandlerResult";
41+
42+
private static final Class<?> CLASS_HANDLER_RESULT = loadClass(TYPE_HANDLER_RESULT, HttpHeaders.class.getClassLoader());
43+
44+
private static final FieldAccessor ACCESSOR_EXCEPTION_HANDLER = getAccessor(CLASS_HANDLER_RESULT, "exceptionHandler");
45+
4046
/**
4147
* Creates writable copy of HTTP headers.
4248
*
@@ -48,7 +54,11 @@ public static HttpHeaders writable(HttpHeaders headers) {
4854
}
4955

5056
public static Object getHandler(Object handlerMethod) {
51-
return handlerMethod != null && CLASS_HANDLER_METHOD.isInstance(handlerMethod) ? ACCESSOR_HANDLER.get(handlerMethod) : null;
57+
return ACCESSOR_HANDLER != null && handlerMethod != null && CLASS_HANDLER_METHOD.isInstance(handlerMethod) ? ACCESSOR_HANDLER.get(handlerMethod) : null;
58+
}
59+
60+
public static <T> T getExceptionHandler(Object handlerResult) {
61+
return ACCESSOR_EXCEPTION_HANDLER != null && handlerResult != null && CLASS_HANDLER_RESULT.isInstance(handlerResult) ? (T) ACCESSOR_EXCEPTION_HANDLER.get(handlerResult) : null;
5262
}
5363

5464
/**

joylive-plugin/joylive-router/joylive-router-springweb6/src/main/java/com/jd/live/agent/plugin/router/springweb/v6/interceptor/DispatcherHandlerExceptionInterceptor.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext;
1919
import com.jd.live.agent.bootstrap.bytekit.context.MethodContext;
2020
import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor;
21+
import com.jd.live.agent.core.util.option.Converts;
2122
import com.jd.live.agent.governance.config.ServiceConfig;
2223
import com.jd.live.agent.plugin.router.springweb.v6.util.CloudUtils;
2324
import org.springframework.http.HttpHeaders;
@@ -45,21 +46,26 @@ public DispatcherHandlerExceptionInterceptor(ServiceConfig config) {
4546

4647
@Override
4748
public void onSuccess(ExecutableContext ctx) {
48-
if (config.isResponseException()) {
49-
MethodContext mc = (MethodContext) ctx;
50-
ServerWebExchange exchange = mc.getArgument(0);
49+
if (!config.isResponseException()) {
50+
// Without exception propagation
51+
return;
52+
}
53+
MethodContext mc = (MethodContext) ctx;
54+
ServerWebExchange exchange = mc.getArgument(0);
55+
boolean live = Converts.getBoolean(exchange.getAttribute(KEY_LIVE_REQUEST), Boolean.FALSE);
56+
if (!live) {
5157
// Agent request
52-
Boolean live = (Boolean) exchange.getAttributes().remove(KEY_LIVE_REQUEST);
53-
if (live != null && live) {
54-
Mono<Void> mono = mc.getResult();
55-
mono = mono.onErrorResume(ex -> {
56-
exchange.getAttributes().put(KEY_LIVE_EXCEPTION_HANDLED, Boolean.TRUE);
57-
HttpHeaders headers = CloudUtils.writable(exchange.getResponse().getHeaders());
58-
labelHeaders(ex, headers::set);
59-
return Mono.error(ex);
60-
});
61-
mc.setResult(mono);
62-
}
58+
return;
6359
}
60+
Mono<Void> mono = mc.getResult();
61+
mono = mono.doOnError(ex -> {
62+
boolean handled = Converts.getBoolean(exchange.getAttribute(KEY_LIVE_EXCEPTION_HANDLED), Boolean.FALSE);
63+
if (!handled) {
64+
exchange.getAttributes().put(KEY_LIVE_EXCEPTION_HANDLED, Boolean.TRUE);
65+
HttpHeaders headers = CloudUtils.writable(exchange.getResponse().getHeaders());
66+
labelHeaders(ex, headers::set);
67+
}
68+
});
69+
mc.setResult(mono);
6470
}
6571
}

joylive-plugin/joylive-router/joylive-router-springweb6/src/main/java/com/jd/live/agent/plugin/router/springweb/v6/interceptor/HandlerAdapterInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public void onEnter(ExecutableContext ctx) {
5959
McpConfig mcpConfig = govnConfig.getMcpConfig();
6060
ServiceConfig serviceConfig = govnConfig.getServiceConfig();
6161
MethodContext mc = (MethodContext) ctx;
62-
ServerWebExchange exchange = (ServerWebExchange) mc.getArguments()[0];
63-
Object handler = mc.getArguments()[1];
62+
ServerWebExchange exchange = mc.getArgument(0);
63+
Object handler = mc.getArgument(1);
6464
ReactiveInboundRequest request = new ReactiveInboundRequest(exchange.getRequest(), handler, serviceConfig::isSystem, mcpConfig::isMcp, parser);
6565
if (!request.isSystem()) {
6666
exchange.getAttributes().put(KEY_LIVE_REQUEST, Boolean.TRUE);

joylive-plugin/joylive-router/joylive-router-springweb7/src/main/java/com/jd/live/agent/plugin/router/springweb/v7/interceptor/DispatcherHandlerExceptionInterceptor.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext;
1919
import com.jd.live.agent.bootstrap.bytekit.context.MethodContext;
2020
import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor;
21+
import com.jd.live.agent.core.util.option.Converts;
2122
import com.jd.live.agent.governance.config.ServiceConfig;
2223
import com.jd.live.agent.plugin.router.springweb.v7.util.CloudUtils;
2324
import org.springframework.http.HttpHeaders;
@@ -45,20 +46,26 @@ public DispatcherHandlerExceptionInterceptor(ServiceConfig config) {
4546

4647
@Override
4748
public void onSuccess(ExecutableContext ctx) {
48-
if (config.isResponseException()) {
49-
MethodContext mc = (MethodContext) ctx;
50-
ServerWebExchange exchange = (ServerWebExchange) mc.getArguments()[0];
51-
Boolean live = (Boolean) exchange.getAttributes().remove(KEY_LIVE_REQUEST);
52-
if (live != null && live) {
53-
Mono<Void> mono = mc.getResult();
54-
mono = mono.onErrorResume(ex -> {
55-
exchange.getAttributes().put(KEY_LIVE_EXCEPTION_HANDLED, Boolean.TRUE);
56-
HttpHeaders headers = CloudUtils.writable(exchange.getResponse().getHeaders());
57-
labelHeaders(ex, headers::set);
58-
return Mono.error(ex);
59-
});
60-
mc.setResult(mono);
61-
}
49+
if (!config.isResponseException()) {
50+
// Without exception propagation
51+
return;
52+
}
53+
MethodContext mc = (MethodContext) ctx;
54+
ServerWebExchange exchange = mc.getArgument(0);
55+
boolean live = Converts.getBoolean(exchange.getAttribute(KEY_LIVE_REQUEST), Boolean.FALSE);
56+
if (!live) {
57+
// Agent request
58+
return;
6259
}
60+
Mono<Void> mono = mc.getResult();
61+
mono = mono.doOnError(ex -> {
62+
boolean handled = Converts.getBoolean(exchange.getAttribute(KEY_LIVE_EXCEPTION_HANDLED), Boolean.FALSE);
63+
if (!handled) {
64+
exchange.getAttributes().put(KEY_LIVE_EXCEPTION_HANDLED, Boolean.TRUE);
65+
HttpHeaders headers = CloudUtils.writable(exchange.getResponse().getHeaders());
66+
labelHeaders(ex, headers::set);
67+
}
68+
});
69+
mc.setResult(mono);
6370
}
6471
}

joylive-plugin/joylive-router/joylive-router-springweb7/src/main/java/com/jd/live/agent/plugin/router/springweb/v7/interceptor/HandlerAdapterInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public void onEnter(ExecutableContext ctx) {
5959
McpConfig mcpConfig = govnConfig.getMcpConfig();
6060
ServiceConfig serviceConfig = govnConfig.getServiceConfig();
6161
MethodContext mc = (MethodContext) ctx;
62-
ServerWebExchange exchange = (ServerWebExchange) mc.getArguments()[0];
63-
Object handler = mc.getArguments()[1];
62+
ServerWebExchange exchange = mc.getArgument(0);
63+
Object handler = mc.getArgument(1);
6464
ReactiveInboundRequest request = new ReactiveInboundRequest(exchange.getRequest(), handler, serviceConfig::isSystem, mcpConfig::isMcp, parser);
6565
if (!request.isSystem()) {
6666
exchange.getAttributes().put(KEY_LIVE_REQUEST, Boolean.TRUE);

0 commit comments

Comments
 (0)