Search in sources :

Example 1 with EzyExceptionHandlers

use of com.tvd12.ezyfox.util.EzyExceptionHandlers in project ezyfox-server by youngmonkeys.

the class EzyHandleExceptionImplTest method handleExceptionWithHandlers.

@Test
public void handleExceptionWithHandlers() {
    // given
    EzyExceptionHandlersFetcher fetcher = mock(EzyExceptionHandlersFetcher.class);
    Exception exception = new IllegalArgumentException("one");
    EzyExceptionHandlers handlers = mock(EzyExceptionHandlers.class);
    when(fetcher.getExceptionHandlers()).thenReturn(handlers);
    EzyHandleExceptionImpl sut = new EzyHandleExceptionImpl(fetcher);
    Logger logger = mock(Logger.class);
    FieldUtil.setFieldValue(sut, "logger", logger);
    // when
    sut.handle(Thread.currentThread(), exception);
    // then
    verify(handlers, times(1)).isEmpty();
    verify(handlers, times(1)).handleException(Thread.currentThread(), exception);
    verify(logger, times(0)).info("there is no handler for exception: ", exception);
}
Also used : EzyExceptionHandlers(com.tvd12.ezyfox.util.EzyExceptionHandlers) Logger(org.slf4j.Logger) EzyHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl) EzyExceptionHandlersFetcher(com.tvd12.ezyfox.util.EzyExceptionHandlersFetcher) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 2 with EzyExceptionHandlers

use of com.tvd12.ezyfox.util.EzyExceptionHandlers in project ezyfox-server by youngmonkeys.

the class EzyHandleExceptionImplTest method handleExceptionWithHandlersButException.

@Test
public void handleExceptionWithHandlersButException() {
    // given
    EzyExceptionHandlersFetcher fetcher = mock(EzyExceptionHandlersFetcher.class);
    Exception exception = new IllegalArgumentException("one");
    EzyExceptionHandlers handlers = mock(EzyExceptionHandlers.class);
    when(fetcher.getExceptionHandlers()).thenReturn(handlers);
    EzyHandleExceptionImpl sut = new EzyHandleExceptionImpl(fetcher);
    Logger logger = mock(Logger.class);
    FieldUtil.setFieldValue(sut, "logger", logger);
    RuntimeException ex = new RuntimeException("just test");
    doThrow(ex).when(handlers).handleException(Thread.currentThread(), exception);
    // when
    sut.handle(Thread.currentThread(), exception);
    // then
    verify(handlers, times(1)).isEmpty();
    verify(handlers, times(1)).handleException(Thread.currentThread(), exception);
    verify(logger, times(1)).warn("handle exception: {} error", exceptionToSimpleString(exception), ex);
}
Also used : EzyExceptionHandlers(com.tvd12.ezyfox.util.EzyExceptionHandlers) Logger(org.slf4j.Logger) EzyHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl) EzyExceptionHandlersFetcher(com.tvd12.ezyfox.util.EzyExceptionHandlersFetcher) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 3 with EzyExceptionHandlers

use of com.tvd12.ezyfox.util.EzyExceptionHandlers in project ezyfox-server by youngmonkeys.

the class EzyAddExceptionHandlerImplTest method test.

@Test
public void test() {
    EzyExceptionHandlersFetcher fetcher = mock(EzyExceptionHandlersFetcher.class);
    EzyExceptionHandlers handlers = mock(EzyExceptionHandlers.class);
    when(fetcher.getExceptionHandlers()).thenReturn(handlers);
    EzyAddExceptionHandlerImpl cmd = new EzyAddExceptionHandlerImpl(fetcher);
    cmd.add(mock(EzyExceptionHandler.class));
}
Also used : EzyExceptionHandlers(com.tvd12.ezyfox.util.EzyExceptionHandlers) EzyExceptionHandler(com.tvd12.ezyfox.util.EzyExceptionHandler) EzyAddExceptionHandlerImpl(com.tvd12.ezyfoxserver.command.impl.EzyAddExceptionHandlerImpl) EzyExceptionHandlersFetcher(com.tvd12.ezyfox.util.EzyExceptionHandlersFetcher) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 4 with EzyExceptionHandlers

use of com.tvd12.ezyfox.util.EzyExceptionHandlers in project ezyfox-server by youngmonkeys.

the class EzyAppHandleExceptionImpl method handle.

@Override
public void handle(Thread thread, Throwable throwable) {
    String appName = app.getSetting().getName();
    EzyExceptionHandlers handlers = fetcher.getExceptionHandlers();
    if (handlers.isEmpty()) {
        logger.info("app: {} has no handler for exception:", appName, throwable);
    } else {
        try {
            handlers.handleException(thread, throwable);
        } catch (Exception e) {
            logger.warn("handle exception: {} on app: {} error", exceptionToSimpleString(throwable), appName, e);
        }
    }
}
Also used : EzyExceptionHandlers(com.tvd12.ezyfox.util.EzyExceptionHandlers) EzyStrings.exceptionToSimpleString(com.tvd12.ezyfox.io.EzyStrings.exceptionToSimpleString) EzyHandleException(com.tvd12.ezyfoxserver.command.EzyHandleException)

Example 5 with EzyExceptionHandlers

use of com.tvd12.ezyfox.util.EzyExceptionHandlers in project ezyfox-server by youngmonkeys.

the class EzyPluginHandleExceptionImpl method handle.

@Override
public void handle(Thread thread, Throwable throwable) {
    String pluginName = plugin.getSetting().getName();
    EzyExceptionHandlers handlers = fetcher.getExceptionHandlers();
    if (handlers.isEmpty()) {
        logger.info("plugin: {} has no handler for exception:", pluginName, throwable);
    } else {
        try {
            handlers.handleException(thread, throwable);
        } catch (Exception e) {
            logger.warn("handle exception: {} on plugin: {} error", EzyStrings.exceptionToSimpleString(throwable), pluginName, e);
        }
    }
}
Also used : EzyExceptionHandlers(com.tvd12.ezyfox.util.EzyExceptionHandlers) EzyHandleException(com.tvd12.ezyfoxserver.command.EzyHandleException)

Aggregations

EzyExceptionHandlers (com.tvd12.ezyfox.util.EzyExceptionHandlers)7 EzyExceptionHandlersFetcher (com.tvd12.ezyfox.util.EzyExceptionHandlersFetcher)4 BaseTest (com.tvd12.test.base.BaseTest)4 Test (org.testng.annotations.Test)4 EzyHandleExceptionImpl (com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl)3 Logger (org.slf4j.Logger)3 EzyHandleException (com.tvd12.ezyfoxserver.command.EzyHandleException)2 EzyStrings.exceptionToSimpleString (com.tvd12.ezyfox.io.EzyStrings.exceptionToSimpleString)1 EzyExceptionHandler (com.tvd12.ezyfox.util.EzyExceptionHandler)1 EzyAddExceptionHandlerImpl (com.tvd12.ezyfoxserver.command.impl.EzyAddExceptionHandlerImpl)1