Skip to content

Commit 6bca219

Browse files
committed
Fix spring web inbound exception
1 parent 2a96037 commit 6bca219

File tree

9 files changed

+49
-12
lines changed

9 files changed

+49
-12
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import javax.servlet.http.HttpServletRequest;
3434

3535
import static com.jd.live.agent.core.util.ExceptionUtils.getCause;
36+
import static com.jd.live.agent.plugin.router.springweb.v5.exception.SpringInboundThrower.THROWER;
3637

3738
/**
3839
* InvocableHandlerInterceptor
@@ -53,14 +54,15 @@ public void onEnter(ExecutableContext ctx) {
5354
if (!(ctx.getTarget() instanceof ServletInvocableHandlerMethod)) {
5455
return;
5556
}
57+
5658
GovernanceConfig govnConfig = context.getGovernanceConfig();
5759
McpConfig mcpConfig = govnConfig.getMcpConfig();
5860
ServiceConfig serviceConfig = govnConfig.getServiceConfig();
5961
MethodContext mc = (MethodContext) ctx;
6062
NativeWebRequest webRequest = ctx.getArgument(0);
6163
HttpServletRequest servletRequest = (HttpServletRequest) webRequest.getNativeRequest();
6264
Object handler = CloudUtils.getHandler(ctx.getTarget());
63-
ServletInboundRequest request = new ServletInboundRequest(servletRequest, handler, serviceConfig::isSystem, mcpConfig::isMcp, parser);
65+
ServletInboundRequest request = new ServletInboundRequest(servletRequest, ctx.getArguments(), handler, serviceConfig::isSystem, mcpConfig::isMcp, parser);
6466
if (!request.isSystem()) {
6567
HttpInboundInvocation<ServletInboundRequest> invocation = new HttpInboundInvocation<>(request, context);
6668
context.inward(invocation, mc::invokeOrigin, (v, e) -> {
@@ -69,7 +71,7 @@ public void onEnter(ExecutableContext ctx) {
6971
} else if (request.isMcp()) {
7072
mc.skipWithResult(JsonRpcResponse.createErrorResponse(request.getMcpRequestId(), getCause(e)));
7173
} else {
72-
mc.skipWithThrowable(e);
74+
mc.skipWithThrowable(THROWER.createException(e, request));
7375
}
7476
});
7577
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import static com.jd.live.agent.core.util.ExceptionUtils.getCause;
3636
import static com.jd.live.agent.core.util.ExceptionUtils.toException;
37+
import static com.jd.live.agent.plugin.router.springweb.v5.exception.SpringInboundThrower.THROWER;
3738

3839
/**
3940
* Interceptor for RouterFunction's HandlerFunction to provide service governance capabilities.
@@ -85,7 +86,7 @@ private void delegate(MethodContext ctx) {
8586
GovernanceConfig govnConfig = context.getGovernanceConfig();
8687
McpConfig mcpConfig = govnConfig.getMcpConfig();
8788
ServiceConfig serviceConfig = govnConfig.getServiceConfig();
88-
ServletInboundRequest request = new ServletInboundRequest(req.servletRequest(), null, serviceConfig::isSystem, mcpConfig::isMcp, parser);
89+
ServletInboundRequest request = new ServletInboundRequest(req.servletRequest(), null, null, serviceConfig::isSystem, mcpConfig::isMcp, parser);
8990
if (!request.isSystem()) {
9091
HttpInboundInvocation<ServletInboundRequest> invocation = new HttpInboundInvocation<>(request, context);
9192
try {
@@ -94,7 +95,7 @@ private void delegate(MethodContext ctx) {
9495
if (request.isMcp()) {
9596
return ServerResponse.ok().body(JsonRpcResponse.createErrorResponse(request.getMcpRequestId(), getCause(e)));
9697
}
97-
throw toException(e);
98+
throw toException(THROWER.createException(e, request));
9899
}
99100
} else {
100101
return delegate.handle(req);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public class ServletInboundRequest extends AbstractHttpInboundRequest<HttpServle
8787

8888
private static final Class<?> SWAGGER_WELCOME_COMMON_CLASS = loadClass(SWAGGER_WELCOME_COMMON_TYPE, HttpServletRequest.class.getClassLoader());
8989

90+
private final Object[] arguments;
91+
9092
private final Object handler;
9193

9294
private final Predicate<String> systemPredicate;
@@ -96,11 +98,13 @@ public class ServletInboundRequest extends AbstractHttpInboundRequest<HttpServle
9698
private final JsonPathParser parser;
9799

98100
public ServletInboundRequest(HttpServletRequest request,
101+
Object[] arguments,
99102
Object handler,
100103
Predicate<String> systemPredicate,
101104
Predicate<String> mcpPredicate,
102105
JsonPathParser parser) {
103106
super(request);
107+
this.arguments = arguments;
104108
this.handler = handler;
105109
this.systemPredicate = systemPredicate;
106110
this.mcpPredicate = mcpPredicate;
@@ -147,6 +151,12 @@ public boolean isSystem() {
147151
}
148152
} else if (ACTUATOR_SERVLET_CLASS != null && ACTUATOR_SERVLET_CLASS.isInstance(handler)) {
149153
return true;
154+
} else if (arguments != null && arguments.length == 3 && arguments[2] instanceof Object[]) {
155+
// ExceptionHandlerExceptionResolver for global @ExceptionHandler(Exception.class)
156+
Object[] args = (Object[]) arguments[2];
157+
if (args.length > 1 && args[0] instanceof Throwable && args[args.length - 1] instanceof HandlerMethod) {
158+
return true;
159+
}
150160
}
151161
}
152162
if (systemPredicate != null && systemPredicate.test(getPath())) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod;
3333

3434
import static com.jd.live.agent.core.util.ExceptionUtils.getCause;
35+
import static com.jd.live.agent.plugin.router.springweb.v6.exception.SpringInboundThrower.THROWER;
3536

3637
/**
3738
* InvocableHandlerInterceptor
@@ -59,7 +60,7 @@ public void onEnter(ExecutableContext ctx) {
5960
NativeWebRequest webRequest = ctx.getArgument(0);
6061
HttpServletRequest servletRequest = (HttpServletRequest) webRequest.getNativeRequest();
6162
Object handler = CloudUtils.getHandler(ctx.getTarget());
62-
ServletInboundRequest request = new ServletInboundRequest(servletRequest, handler, serviceConfig::isSystem, mcpConfig::isMcp, parser);
63+
ServletInboundRequest request = new ServletInboundRequest(servletRequest, ctx.getArguments(), handler, serviceConfig::isSystem, mcpConfig::isMcp, parser);
6364
if (!request.isSystem()) {
6465
HttpInboundInvocation<ServletInboundRequest> invocation = new HttpInboundInvocation<>(request, context);
6566
context.inward(invocation, mc::invokeOrigin, (v, e) -> {
@@ -68,7 +69,7 @@ public void onEnter(ExecutableContext ctx) {
6869
} else if (request.isMcp()) {
6970
mc.skipWithResult(JsonRpcResponse.createErrorResponse(request.getMcpRequestId(), getCause(e)));
7071
} else {
71-
mc.skipWithThrowable(e);
72+
mc.skipWithThrowable(THROWER.createException(e, request));
7273
}
7374
});
7475
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import static com.jd.live.agent.core.util.ExceptionUtils.getCause;
3636
import static com.jd.live.agent.core.util.ExceptionUtils.toException;
37+
import static com.jd.live.agent.plugin.router.springweb.v6.exception.SpringInboundThrower.THROWER;
3738

3839
/**
3940
* Interceptor for RouterFunction's HandlerFunction to provide service governance capabilities.
@@ -85,7 +86,7 @@ private void delegate(MethodContext ctx) {
8586
GovernanceConfig govnConfig = context.getGovernanceConfig();
8687
McpConfig mcpConfig = govnConfig.getMcpConfig();
8788
ServiceConfig serviceConfig = govnConfig.getServiceConfig();
88-
ServletInboundRequest request = new ServletInboundRequest(req.servletRequest(), null, serviceConfig::isSystem, mcpConfig::isMcp, parser);
89+
ServletInboundRequest request = new ServletInboundRequest(req.servletRequest(), null, null, serviceConfig::isSystem, mcpConfig::isMcp, parser);
8990
if (!request.isSystem()) {
9091
HttpInboundInvocation<ServletInboundRequest> invocation = new HttpInboundInvocation<>(request, context);
9192
try {
@@ -94,7 +95,7 @@ private void delegate(MethodContext ctx) {
9495
if (request.isMcp()) {
9596
return ServerResponse.ok().body(JsonRpcResponse.createErrorResponse(request.getMcpRequestId(), getCause(e)));
9697
}
97-
throw toException(e);
98+
throw toException(THROWER.createException(e, request));
9899
}
99100
} else {
100101
return delegate.handle(req);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,22 @@ public class ServletInboundRequest extends AbstractHttpInboundRequest<HttpServle
8989

9090
private final Object handler;
9191

92+
private final Object[] arguments;
93+
9294
private final Predicate<String> systemPredicate;
9395

9496
private final Predicate<String> mcpPredicate;
9597

9698
private final JsonPathParser parser;
9799

98100
public ServletInboundRequest(HttpServletRequest request,
101+
Object[] arguments,
99102
Object handler,
100103
Predicate<String> systemPredicate,
101104
Predicate<String> mcpPredicate,
102105
JsonPathParser parser) {
103106
super(request);
107+
this.arguments = arguments;
104108
this.handler = handler;
105109
this.systemPredicate = systemPredicate;
106110
this.mcpPredicate = mcpPredicate;
@@ -147,6 +151,12 @@ public boolean isSystem() {
147151
}
148152
} else if (ACTUATOR_SERVLET_CLASS != null && ACTUATOR_SERVLET_CLASS.isInstance(handler)) {
149153
return true;
154+
} else if (arguments != null && arguments.length == 3 && arguments[2] instanceof Object[]) {
155+
// ExceptionHandlerExceptionResolver for global @ExceptionHandler(Exception.class)
156+
Object[] args = (Object[]) arguments[2];
157+
if (args.length > 1 && args[0] instanceof Throwable && args[args.length - 1] instanceof HandlerMethod) {
158+
return true;
159+
}
150160
}
151161
}
152162
if (systemPredicate != null && systemPredicate.test(getPath())) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod;
3333

3434
import static com.jd.live.agent.core.util.ExceptionUtils.getCause;
35+
import static com.jd.live.agent.plugin.router.springweb.v7.exception.SpringInboundThrower.THROWER;
3536

3637
/**
3738
* InvocableHandlerInterceptor
@@ -59,7 +60,7 @@ public void onEnter(ExecutableContext ctx) {
5960
NativeWebRequest webRequest = ctx.getArgument(0);
6061
HttpServletRequest servletRequest = (HttpServletRequest) webRequest.getNativeRequest();
6162
Object handler = CloudUtils.getHandler(ctx.getTarget());
62-
ServletInboundRequest request = new ServletInboundRequest(servletRequest, handler, serviceConfig::isSystem, mcpConfig::isMcp, parser);
63+
ServletInboundRequest request = new ServletInboundRequest(servletRequest, ctx.getArguments(), handler, serviceConfig::isSystem, mcpConfig::isMcp, parser);
6364
if (!request.isSystem()) {
6465
HttpInboundInvocation<ServletInboundRequest> invocation = new HttpInboundInvocation<>(request, context);
6566
context.inward(invocation, mc::invokeOrigin, (v, e) -> {
@@ -68,7 +69,7 @@ public void onEnter(ExecutableContext ctx) {
6869
} else if (request.isMcp()) {
6970
mc.skipWithResult(JsonRpcResponse.createErrorResponse(request.getMcpRequestId(), getCause(e)));
7071
} else {
71-
mc.skipWithThrowable(e);
72+
mc.skipWithThrowable(THROWER.createException(e, request));
7273
}
7374
});
7475
}

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

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

3636
import static com.jd.live.agent.core.util.ExceptionUtils.getCause;
3737
import static com.jd.live.agent.core.util.ExceptionUtils.toException;
38+
import static com.jd.live.agent.plugin.router.springweb.v7.exception.SpringInboundThrower.THROWER;
3839

3940
/**
4041
* Interceptor for RouterFunction's HandlerFunction to provide service governance capabilities.
@@ -87,7 +88,7 @@ private void delegate(MethodContext ctx) {
8788
McpConfig mcpConfig = govnConfig.getMcpConfig();
8889
ServiceConfig serviceConfig = govnConfig.getServiceConfig();
8990
HttpServletRequest servletRequest = req.servletRequest();
90-
ServletInboundRequest request = new ServletInboundRequest(servletRequest, null, serviceConfig::isSystem, mcpConfig::isMcp, parser);
91+
ServletInboundRequest request = new ServletInboundRequest(servletRequest, ctx.getArguments(), null, serviceConfig::isSystem, mcpConfig::isMcp, parser);
9192
if (!request.isSystem()) {
9293
HttpInboundInvocation<ServletInboundRequest> invocation = new HttpInboundInvocation<>(request, context);
9394
try {
@@ -96,7 +97,7 @@ private void delegate(MethodContext ctx) {
9697
if (request.isMcp()) {
9798
return ServerResponse.ok().body(JsonRpcResponse.createErrorResponse(request.getMcpRequestId(), getCause(e)));
9899
}
99-
throw toException(e);
100+
throw toException(THROWER.createException(e, request));
100101
}
101102
} else {
102103
return delegate.handle(req);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,22 @@ public class ServletInboundRequest extends AbstractHttpInboundRequest<HttpServle
8989

9090
private final Object handler;
9191

92+
private final Object[] arguments;
93+
9294
private final Predicate<String> systemPredicate;
9395

9496
private final Predicate<String> mcpPredicate;
9597

9698
private final JsonPathParser parser;
9799

98100
public ServletInboundRequest(HttpServletRequest request,
101+
Object[] arguments,
99102
Object handler,
100103
Predicate<String> systemPredicate,
101104
Predicate<String> mcpPredicate,
102105
JsonPathParser parser) {
103106
super(request);
107+
this.arguments = arguments;
104108
this.handler = handler;
105109
this.systemPredicate = systemPredicate;
106110
this.mcpPredicate = mcpPredicate;
@@ -147,6 +151,12 @@ public boolean isSystem() {
147151
}
148152
} else if (ACTUATOR_SERVLET_CLASS != null && ACTUATOR_SERVLET_CLASS.isInstance(handler)) {
149153
return true;
154+
} else if (arguments != null && arguments.length == 3 && arguments[2] instanceof Object[]) {
155+
// ExceptionHandlerExceptionResolver for global @ExceptionHandler(Exception.class)
156+
Object[] args = (Object[]) arguments[2];
157+
if (args.length > 1 && args[0] instanceof Throwable && args[args.length - 1] instanceof HandlerMethod) {
158+
return true;
159+
}
150160
}
151161
}
152162
if (systemPredicate != null && systemPredicate.test(getPath())) {

0 commit comments

Comments
 (0)