[MNG-8082] Exceptions of proxied SessionScoped components are not working correctly (#1449)

Signed-off-by: Jonas Rutishauser <jonas.rutishauser@alumni.ethz.ch>
This commit is contained in:
Jonas Rutishauser 2024-04-23 14:52:20 +02:00 committed by GitHub
parent 860310b692
commit aae74dfbee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View File

@ -107,7 +107,11 @@ public class SessionScope implements Scope {
private <T> T createProxy(Key<T> key, Provider<T> unscoped) { private <T> T createProxy(Key<T> key, Provider<T> unscoped) {
InvocationHandler dispatcher = (proxy, method, args) -> { InvocationHandler dispatcher = (proxy, method, args) -> {
method.setAccessible(true); method.setAccessible(true);
return method.invoke(getScopeState().scope(key, unscoped).get(), args); try {
return method.invoke(getScopeState().scope(key, unscoped).get(), args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}; };
Class<T> superType = (Class<T>) key.getTypeLiteral().getRawType(); Class<T> superType = (Class<T>) key.getTypeLiteral().getRawType();
Class<?>[] interfaces = getInterfaces(superType); Class<?>[] interfaces = getInterfaces(superType);

View File

@ -74,6 +74,7 @@ public class SessionScopeProxyTest {
assertNotNull(bean.myBean.getSession()); assertNotNull(bean.myBean.getSession());
assertNotNull(bean.myBean.getAnotherBean()); assertNotNull(bean.myBean.getAnotherBean());
assertSame(bean.myBean.getAnotherBean().getClass(), AnotherBean.class); assertSame(bean.myBean.getAnotherBean().getClass(), AnotherBean.class);
assertThrows(TestException.class, () -> bean.myBean.throwException());
} }
@Named @Named
@ -102,6 +103,8 @@ public class SessionScopeProxyTest {
Session getSession(); Session getSession();
BeanItf2 getAnotherBean(); BeanItf2 getAnotherBean();
void throwException() throws TestException;
} }
interface BeanItf2 {} interface BeanItf2 {}
@ -127,5 +130,11 @@ public class SessionScopeProxyTest {
public BeanItf2 getAnotherBean() { public BeanItf2 getAnotherBean() {
return anotherBean; return anotherBean;
} }
public void throwException() throws TestException {
throw new TestException();
}
} }
static class TestException extends Exception {}
} }