Skip to content

Commit 07ff40a

Browse files
Thermoweb bugfix/2246 handle invocation target exception (#2264)
* Fixes gh-2246 : handle InvocationTargetException properly * Polished the solution + added tests --------- Co-authored-by: romain.chauveau <[email protected]>
1 parent a01ab50 commit 07ff40a

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/session/TraceSessionRepositoryAspect.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private <T> Object callMethodOnWrappedObject(ProceedingJoinPoint pjp, T target)
7979
if (log.isDebugEnabled()) {
8080
log.debug("Found a corresponding method on the trace representation [" + method + "]");
8181
}
82-
return method.invoke(target, pjp.getArgs());
82+
return ReflectionUtils.invokeMethod(method, target, pjp.getArgs());
8383
}
8484
if (log.isDebugEnabled()) {
8585
log.debug("Method [" + pjp.getSignature().getName()
@@ -98,7 +98,7 @@ public Object wrapReactiveSessionRepository(ProceedingJoinPoint pjp) throws Thro
9898
return callMethodOnWrappedObject(pjp, target);
9999
}
100100

101-
private Method getMethod(ProceedingJoinPoint pjp, Object tracingWrapper) {
101+
Method getMethod(ProceedingJoinPoint pjp, Object tracingWrapper) {
102102
MethodSignature signature = (MethodSignature) pjp.getSignature();
103103
Method method = signature.getMethod();
104104
Method foundMethodOnTracingWrapper = ReflectionUtils.findMethod(tracingWrapper.getClass(), method.getName(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2013-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.sleuth.instrument.session;
18+
19+
import java.lang.reflect.Method;
20+
21+
import org.aspectj.lang.ProceedingJoinPoint;
22+
import org.assertj.core.api.BDDAssertions;
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.cloud.sleuth.tracer.SimpleTracer;
26+
import org.springframework.session.SessionRepository;
27+
28+
import static org.mockito.BDDMockito.given;
29+
import static org.mockito.Mockito.mock;
30+
31+
class TraceSessionRepositoryAspectTests {
32+
33+
@Test
34+
void should_throw_the_cause_of_the_session_exception() {
35+
SimpleTracer simpleTracer = new SimpleTracer();
36+
TraceSessionRepositoryAspect aspect = new TraceSessionRepositoryAspect(simpleTracer,
37+
simpleTracer.currentTraceContext()) {
38+
@Override
39+
Method getMethod(ProceedingJoinPoint pjp, Object tracingWrapper) {
40+
try {
41+
return SessionRepository.class.getMethod("createSession");
42+
}
43+
catch (NoSuchMethodException e) {
44+
throw new RuntimeException(e);
45+
}
46+
}
47+
};
48+
ProceedingJoinPoint pjp = mock(ProceedingJoinPoint.class);
49+
SessionRepository sessionRepository = mock(SessionRepository.class);
50+
given(pjp.getTarget()).willReturn(sessionRepository);
51+
given(sessionRepository.createSession()).willThrow(new RuntimeException("Boom!"));
52+
53+
BDDAssertions.thenThrownBy(() -> aspect.wrapSessionRepository(pjp)).isInstanceOf(RuntimeException.class)
54+
.hasMessageContaining("Boom!");
55+
}
56+
57+
}

0 commit comments

Comments
 (0)