Search in sources :

Example 1 with EzyExceptionHandler

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

the class EzyPluginHandleExceptionImplTest method handleExceptionWithHandlers.

@Test
public void handleExceptionWithHandlers() {
    // given
    EzySimplePlugin plugin = new EzySimplePlugin();
    EzySimplePluginSetting setting = new EzySimplePluginSetting();
    String pluginName = RandomUtil.randomShortAlphabetString();
    setting.setName(pluginName);
    plugin.setSetting(setting);
    EzyPluginHandleExceptionImpl sut = new EzyPluginHandleExceptionImpl(plugin);
    Logger logger = mock(Logger.class);
    FieldUtil.setFieldValue(sut, "logger", logger);
    EzyExceptionHandler exceptionHandler = mock(EzyExceptionHandler.class);
    plugin.getExceptionHandlers().addExceptionHandler(exceptionHandler);
    // when
    Exception exception = new IllegalArgumentException("one");
    sut.handle(Thread.currentThread(), exception);
    // then
    verify(exceptionHandler, times(1)).handleException(Thread.currentThread(), exception);
    verify(logger, times(0)).info("plugin: {} has no handler for exception:", pluginName, exception);
}
Also used : EzyPluginHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyPluginHandleExceptionImpl) EzyExceptionHandler(com.tvd12.ezyfox.util.EzyExceptionHandler) EzySimplePlugin(com.tvd12.ezyfoxserver.EzySimplePlugin) Logger(org.slf4j.Logger) EzySimplePluginSetting(com.tvd12.ezyfoxserver.setting.EzySimplePluginSetting) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 2 with EzyExceptionHandler

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

the class EzySimpleDataHandler method exceptionCaught.

public void exceptionCaught(Throwable cause) {
    logger.debug("exception caught at session: {}", session, cause);
    EzyExceptionHandler exceptionHandler = exceptionHandlers.get(cause.getClass());
    if (exceptionHandler != null) {
        exceptionHandler.handleException(Thread.currentThread(), cause);
    }
}
Also used : EzyExceptionHandler(com.tvd12.ezyfox.util.EzyExceptionHandler)

Example 3 with EzyExceptionHandler

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

the class EzyAddExceptionHandlerImpl method add.

@Override
public void add(EzyExceptionHandler handler) {
    EzyExceptionHandlers handlers = fetcher.getExceptionHandlers();
    handlers.addExceptionHandler(handler);
}
Also used : EzyExceptionHandlers(com.tvd12.ezyfox.util.EzyExceptionHandlers)

Example 4 with EzyExceptionHandler

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

the class EzyAppHandleExceptionImplTest method handleExceptionWithHandlers.

@Test
public void handleExceptionWithHandlers() {
    // given
    EzySimpleApplication app = new EzySimpleApplication();
    EzySimpleAppSetting setting = new EzySimpleAppSetting();
    String appName = RandomUtil.randomShortAlphabetString();
    setting.setName(appName);
    app.setSetting(setting);
    EzyAppHandleExceptionImpl sut = new EzyAppHandleExceptionImpl(app);
    Logger logger = mock(Logger.class);
    FieldUtil.setFieldValue(sut, "logger", logger);
    EzyExceptionHandler exceptionHandler = mock(EzyExceptionHandler.class);
    app.getExceptionHandlers().addExceptionHandler(exceptionHandler);
    // when
    Exception exception = new IllegalArgumentException("one");
    sut.handle(Thread.currentThread(), exception);
    // then
    verify(exceptionHandler, times(1)).handleException(Thread.currentThread(), exception);
    verify(logger, times(0)).info("app: {} has no handler for exception:", appName, exception);
}
Also used : EzySimpleAppSetting(com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting) EzySimpleApplication(com.tvd12.ezyfoxserver.EzySimpleApplication) EzyExceptionHandler(com.tvd12.ezyfox.util.EzyExceptionHandler) EzyAppHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyAppHandleExceptionImpl) EzyStrings.exceptionToSimpleString(com.tvd12.ezyfox.io.EzyStrings.exceptionToSimpleString) Logger(org.slf4j.Logger) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 5 with EzyExceptionHandler

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

the class EzyAppHandleExceptionImplTest method handleExceptionWithHandlersButException.

@Test
public void handleExceptionWithHandlersButException() {
    // given
    EzySimpleApplication app = new EzySimpleApplication();
    EzySimpleAppSetting setting = new EzySimpleAppSetting();
    String appName = RandomUtil.randomShortAlphabetString();
    setting.setName(appName);
    app.setSetting(setting);
    EzyAppHandleExceptionImpl sut = new EzyAppHandleExceptionImpl(app);
    Logger logger = mock(Logger.class);
    FieldUtil.setFieldValue(sut, "logger", logger);
    Exception exception = new IllegalArgumentException("one");
    EzyExceptionHandler exceptionHandler = mock(EzyExceptionHandler.class);
    RuntimeException ex = new RuntimeException("just test");
    doThrow(ex).when(exceptionHandler).handleException(Thread.currentThread(), exception);
    app.getExceptionHandlers().addExceptionHandler(exceptionHandler);
    // when
    sut.handle(Thread.currentThread(), exception);
    // then
    verify(exceptionHandler, times(1)).handleException(Thread.currentThread(), exception);
    verify(logger, times(1)).warn("handle exception: {} on app: {} error", exceptionToSimpleString(exception), appName, ex);
}
Also used : EzySimpleAppSetting(com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting) EzySimpleApplication(com.tvd12.ezyfoxserver.EzySimpleApplication) EzyExceptionHandler(com.tvd12.ezyfox.util.EzyExceptionHandler) EzyAppHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyAppHandleExceptionImpl) EzyStrings.exceptionToSimpleString(com.tvd12.ezyfox.io.EzyStrings.exceptionToSimpleString) Logger(org.slf4j.Logger) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Aggregations

EzyExceptionHandler (com.tvd12.ezyfox.util.EzyExceptionHandler)5 BaseTest (com.tvd12.test.base.BaseTest)4 Logger (org.slf4j.Logger)4 Test (org.testng.annotations.Test)4 EzyStrings.exceptionToSimpleString (com.tvd12.ezyfox.io.EzyStrings.exceptionToSimpleString)2 EzySimpleApplication (com.tvd12.ezyfoxserver.EzySimpleApplication)2 EzySimplePlugin (com.tvd12.ezyfoxserver.EzySimplePlugin)2 EzyAppHandleExceptionImpl (com.tvd12.ezyfoxserver.command.impl.EzyAppHandleExceptionImpl)2 EzyPluginHandleExceptionImpl (com.tvd12.ezyfoxserver.command.impl.EzyPluginHandleExceptionImpl)2 EzySimpleAppSetting (com.tvd12.ezyfoxserver.setting.EzySimpleAppSetting)2 EzySimplePluginSetting (com.tvd12.ezyfoxserver.setting.EzySimplePluginSetting)2 EzyExceptionHandlers (com.tvd12.ezyfox.util.EzyExceptionHandlers)1