Search in sources :

Example 1 with RemoteException

use of com.haulmont.cuba.core.global.RemoteException in project cuba by cuba-platform.

the class DesktopTimer method handleTimerException.

protected void handleTimerException(RuntimeException ex) {
    if (ExceptionUtils.indexOfType(ex, java.net.ConnectException.class) > -1) {
        // If a ConnectException occurred, just log it and ignore
        log.warn("onTimer error: " + ex.getMessage());
    } else {
        // Otherwise throw the exception, but first search for NoUserSessionException in chain,
        // if found - stop the timer
        int reIdx = ExceptionUtils.indexOfType(ex, RemoteException.class);
        if (reIdx > -1) {
            RemoteException re = (RemoteException) ExceptionUtils.getThrowableList(ex).get(reIdx);
            for (RemoteException.Cause cause : re.getCauses()) {
                // noinspection ThrowableResultOfMethodCallIgnored
                if (cause.getThrowable() instanceof NoUserSessionException) {
                    log.warn("NoUserSessionException in timer, timer will be stopped");
                    disposeTimer();
                    break;
                }
            }
        } else if (ExceptionUtils.indexOfThrowable(ex, NoUserSessionException.class) > -1) {
            log.warn("NoUserSessionException in timer, timer will be stopped");
            disposeTimer();
        }
        throw ex;
    }
}
Also used : RemoteException(com.haulmont.cuba.core.global.RemoteException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Example 2 with RemoteException

use of com.haulmont.cuba.core.global.RemoteException in project cuba by cuba-platform.

the class AbstractExceptionHandler method handle.

@Override
public boolean handle(ErrorEvent event, App app) {
    Throwable exception = event.getThrowable();
    // noinspection unchecked
    List<Throwable> list = ExceptionUtils.getThrowableList(exception);
    for (Throwable throwable : list) {
        if (classNames.contains(throwable.getClass().getName()) && canHandle(throwable.getClass().getName(), throwable.getMessage(), throwable)) {
            doHandle(app, throwable.getClass().getName(), throwable.getMessage(), throwable);
            return true;
        }
        if (throwable instanceof RemoteException) {
            RemoteException remoteException = (RemoteException) throwable;
            for (RemoteException.Cause cause : remoteException.getCauses()) {
                if (classNames.contains(cause.getClassName()) && canHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable())) {
                    doHandle(app, cause.getClassName(), cause.getMessage(), cause.getThrowable());
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : RemoteException(com.haulmont.cuba.core.global.RemoteException)

Example 3 with RemoteException

use of com.haulmont.cuba.core.global.RemoteException in project cuba by cuba-platform.

the class ServiceInterceptorTest method testNewThread.

@Test
public void testNewThread() throws Exception {
    ServiceInterceptorTestService service = AppBeans.get(ServiceInterceptorTestService.class);
    UserSessions userSessions = AppBeans.get(UserSessions.class);
    // workaround for test security setup
    Field startedField = AppContext.class.getDeclaredField("started");
    startedField.setAccessible(true);
    startedField.set(null, true);
    AppContext.setSecurityContext(AppContext.NO_USER_CONTEXT);
    UserSession userSession = new UserSession(AppContext.NO_USER_CONTEXT.getSessionId(), new User(), Collections.emptyList(), Locale.ENGLISH, true);
    userSessions.add(userSession);
    try {
        appender.getMessages().clear();
        service.declarativeTransactionNewThread();
        assertEquals(0, appender.getMessages().stream().filter(s -> s.contains("from another service")).count());
        appender.getMessages().clear();
        try {
            service.executeWithExceptionNewThread();
        } catch (Exception e) {
            assertTrue(e instanceof RemoteException && ((RemoteException) e).getFirstCauseException() instanceof TestingService.TestException);
        }
    } finally {
        userSessions.remove(userSession);
        startedField.set(null, false);
    }
}
Also used : Field(java.lang.reflect.Field) User(com.haulmont.cuba.security.entity.User) UserSession(com.haulmont.cuba.security.global.UserSession) TestingService(com.haulmont.cuba.core.app.TestingService) RemoteException(com.haulmont.cuba.core.global.RemoteException) UserSessions(com.haulmont.cuba.security.app.UserSessions) RemoteException(com.haulmont.cuba.core.global.RemoteException) Test(org.junit.Test)

Example 4 with RemoteException

use of com.haulmont.cuba.core.global.RemoteException in project cuba by cuba-platform.

the class ServiceInterceptorTest method testExceptionHandling.

@Test
public void testExceptionHandling() throws Exception {
    TestingService service1 = AppBeans.get(TestingService.class);
    ServiceInterceptorTestService service2 = AppBeans.get(ServiceInterceptorTestService.class);
    appender.getMessages().clear();
    try {
        service1.executeWithException();
    } catch (Exception e) {
        assertTrue(e instanceof RemoteException && ((RemoteException) e).getFirstCauseException() instanceof TestingService.TestException);
    }
    appender.getMessages().clear();
    try {
        service2.executeWithException();
    } catch (Exception e) {
        assertTrue(e instanceof RemoteException && ((RemoteException) e).getFirstCauseException() instanceof TestingService.TestException);
    }
}
Also used : TestingService(com.haulmont.cuba.core.app.TestingService) RemoteException(com.haulmont.cuba.core.global.RemoteException) RemoteException(com.haulmont.cuba.core.global.RemoteException) Test(org.junit.Test)

Example 5 with RemoteException

use of com.haulmont.cuba.core.global.RemoteException in project cuba by cuba-platform.

the class CubaTimer method handleOnTimerException.

protected void handleOnTimerException(RuntimeException e) {
    int reIdx = ExceptionUtils.indexOfType(e, RemoteException.class);
    if (reIdx > -1) {
        RemoteException re = (RemoteException) ExceptionUtils.getThrowableList(e).get(reIdx);
        for (RemoteException.Cause cause : re.getCauses()) {
            // noinspection ThrowableResultOfMethodCallIgnored
            if (cause.getThrowable() instanceof NoUserSessionException) {
                log.warn("NoUserSessionException in timer {}, timer will be stopped", getLoggingTimerId());
                stop();
                break;
            }
        }
    } else if (ExceptionUtils.indexOfThrowable(e, NoUserSessionException.class) > -1) {
        log.warn("NoUserSessionException in timer {}, timer will be stopped", getLoggingTimerId());
        stop();
    }
    throw e;
}
Also used : RemoteException(com.haulmont.cuba.core.global.RemoteException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Aggregations

RemoteException (com.haulmont.cuba.core.global.RemoteException)5 TestingService (com.haulmont.cuba.core.app.TestingService)2 NoUserSessionException (com.haulmont.cuba.security.global.NoUserSessionException)2 Test (org.junit.Test)2 UserSessions (com.haulmont.cuba.security.app.UserSessions)1 User (com.haulmont.cuba.security.entity.User)1 UserSession (com.haulmont.cuba.security.global.UserSession)1 Field (java.lang.reflect.Field)1