Search in sources :

Example 1 with EzyStrings.exceptionToSimpleString

use of com.tvd12.ezyfox.io.EzyStrings.exceptionToSimpleString 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)

Example 2 with EzyStrings.exceptionToSimpleString

use of com.tvd12.ezyfox.io.EzyStrings.exceptionToSimpleString in project ezyfox-server by youngmonkeys.

the class EzyPluginHandleExceptionImplTest method handleExceptionWithHandlersButException.

@Test
public void handleExceptionWithHandlersButException() {
    // 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);
    Exception exception = new IllegalArgumentException("one");
    EzyExceptionHandler exceptionHandler = mock(EzyExceptionHandler.class);
    RuntimeException ex = new RuntimeException("just test");
    doThrow(ex).when(exceptionHandler).handleException(Thread.currentThread(), exception);
    plugin.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 plugin: {} error", EzyStrings.exceptionToSimpleString(exception), pluginName, ex);
}
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)

Aggregations

EzyExceptionHandler (com.tvd12.ezyfox.util.EzyExceptionHandler)1 EzyExceptionHandlers (com.tvd12.ezyfox.util.EzyExceptionHandlers)1 EzySimplePlugin (com.tvd12.ezyfoxserver.EzySimplePlugin)1 EzyHandleException (com.tvd12.ezyfoxserver.command.EzyHandleException)1 EzyPluginHandleExceptionImpl (com.tvd12.ezyfoxserver.command.impl.EzyPluginHandleExceptionImpl)1 EzySimplePluginSetting (com.tvd12.ezyfoxserver.setting.EzySimplePluginSetting)1 BaseTest (com.tvd12.test.base.BaseTest)1 Logger (org.slf4j.Logger)1 Test (org.testng.annotations.Test)1