Search in sources :

Example 1 with EzyUncaughtExceptionHandler

use of com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler in project ezyfox-server by youngmonkeys.

the class EzyExceptionHandlerImplementer method doImplement.

protected EzyUncaughtExceptionHandler doImplement() throws Exception {
    ClassPool pool = ClassPool.getDefault();
    String implClassName = getImplClassName();
    CtClass implClass = pool.makeClass(implClassName);
    EzyClass superClass = new EzyClass(getSuperClass());
    String exceptionHandlerFieldContent = makeExceptionHandlerFieldContent();
    String setExceptionHandlerMethodContent = makeSetExceptionHandlerMethodContent();
    String handleExceptionMethodContent = makeHandleExceptionMethodContent();
    printComponentContent(implClassName);
    printComponentContent(exceptionHandlerFieldContent);
    printComponentContent(setExceptionHandlerMethodContent);
    printComponentContent(handleExceptionMethodContent);
    implClass.setSuperclass(pool.get(superClass.getName()));
    implClass.addField(CtField.make(exceptionHandlerFieldContent, implClass));
    implClass.addMethod(CtNewMethod.make(setExceptionHandlerMethodContent, implClass));
    implClass.addMethod(CtNewMethod.make(handleExceptionMethodContent, implClass));
    Class answerClass = implClass.toClass();
    implClass.detach();
    EzyAsmUncaughtExceptionHandler handler = (EzyAsmUncaughtExceptionHandler) answerClass.newInstance();
    setRepoComponent(handler);
    return handler;
}
Also used : CtClass(javassist.CtClass) EzyClass(com.tvd12.ezyfox.reflect.EzyClass) ClassPool(javassist.ClassPool) CtClass(javassist.CtClass) EzyClass(com.tvd12.ezyfox.reflect.EzyClass)

Example 2 with EzyUncaughtExceptionHandler

use of com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler in project ezyfox-server by youngmonkeys.

the class EzyUserRequestSingletonController method handle.

public void handle(C context, E event) {
    EzyArray data = event.getData();
    String cmd = data.get(0, String.class);
    EzyUserRequestHandler handler = requestHandlers.get(cmd);
    if (handler == null) {
        prototypeController.handle(context, event);
        return;
    }
    Object handlerData = data.get(1, EzyData.class, null);
    Class requestDataType = handler.getDataType();
    if (requestDataType != null) {
        handlerData = unmarshaller.unmarshal(handlerData, requestDataType);
    }
    try {
        preHandle(context, event, cmd, handlerData);
        handler.handle(context, event, handlerData);
        postHandle(context, event, cmd, handlerData);
    } catch (Exception e) {
        postHandle(context, event, cmd, handlerData, e);
        if (e instanceof EzyBadRequestException) {
            EzyBadRequestException ex = (EzyBadRequestException) e;
            if (ex.isSendToClient()) {
                EzyData errorData = newErrorData(ex);
                responseError(context, event, errorData);
            }
            logger.debug("request cmd: {} by session: {} with data: {} error", cmd, event.getSession().getName(), data, e);
        } else {
            EzyUncaughtExceptionHandler exceptionHandler = getExceptionHandler(e.getClass());
            if (exceptionHandler == null) {
                throw new EzyUserRequestException(cmd, handlerData, e);
            }
            try {
                exceptionHandler.handleException(context, event, cmd, handlerData, e);
            } catch (Exception ex) {
                throw new EzyUserRequestException(cmd, handlerData, ex);
            }
        }
    }
}
Also used : EzyUserRequestException(com.tvd12.ezyfoxserver.support.exception.EzyUserRequestException) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyUserRequestHandler(com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler) EzyData(com.tvd12.ezyfox.entity.EzyData) EzyUncaughtExceptionHandler(com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler) EzyBadRequestException(com.tvd12.ezyfox.core.exception.EzyBadRequestException) EzyUserRequestException(com.tvd12.ezyfoxserver.support.exception.EzyUserRequestException) EzyBadRequestException(com.tvd12.ezyfox.core.exception.EzyBadRequestException)

Example 3 with EzyUncaughtExceptionHandler

use of com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler in project ezyfox-server by youngmonkeys.

the class EzyExceptionHandlersImplementer method implement.

public Map<Class<?>, EzyUncaughtExceptionHandler> implement(Object exceptionHandler) {
    Map<Class<?>, EzyUncaughtExceptionHandler> handlers = new HashMap<>();
    EzyExceptionHandlerProxy proxy = new EzyExceptionHandlerProxy(exceptionHandler);
    for (EzyExceptionHandlerMethod method : proxy.getExceptionHandlerMethods()) {
        EzyExceptionHandlerImplementer implementer = newImplementer(proxy, method);
        EzyUncaughtExceptionHandler handler = implementer.implement();
        for (Class<?> exceptionClass : method.getExceptionClasses()) {
            handlers.put(exceptionClass, handler);
        }
    }
    return handlers;
}
Also used : EzyExceptionHandlerProxy(com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerProxy) HashMap(java.util.HashMap) EzyExceptionHandlerMethod(com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod) EzyUncaughtExceptionHandler(com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler)

Example 4 with EzyUncaughtExceptionHandler

use of com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler in project ezyfox-server by youngmonkeys.

the class EzyExceptionHandlerImplementerTest method testFailedCase.

@Test(expectedExceptions = IllegalStateException.class)
public void testFailedCase() throws Exception {
    EzyExceptionHandlerProxy handlerProxy = new EzyExceptionHandlerProxy(new ExceptionHandlerFail());
    EzyExceptionHandlerMethod method = new EzyExceptionHandlerMethod(new EzyMethod(ExceptionHandlerFail.class.getDeclaredMethod("handle", Exception.class, int.class)));
    new EzyExceptionHandlerImplementer(handlerProxy, method) {

        @SuppressWarnings("rawtypes")
        @Override
        protected EzyUncaughtExceptionHandler doImplement() {
            throw new IllegalStateException("test");
        }
    }.implement();
}
Also used : EzyExceptionHandlerImplementer(com.tvd12.ezyfoxserver.support.asm.EzyExceptionHandlerImplementer) EzyExceptionHandlerProxy(com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerProxy) EzyExceptionHandlerMethod(com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod) EzyUncaughtExceptionHandler(com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler) EzyMethod(com.tvd12.ezyfox.reflect.EzyMethod) Test(org.testng.annotations.Test)

Aggregations

EzyUncaughtExceptionHandler (com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler)3 EzyExceptionHandlerMethod (com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod)2 EzyExceptionHandlerProxy (com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerProxy)2 EzyBadRequestException (com.tvd12.ezyfox.core.exception.EzyBadRequestException)1 EzyArray (com.tvd12.ezyfox.entity.EzyArray)1 EzyData (com.tvd12.ezyfox.entity.EzyData)1 EzyClass (com.tvd12.ezyfox.reflect.EzyClass)1 EzyMethod (com.tvd12.ezyfox.reflect.EzyMethod)1 EzyExceptionHandlerImplementer (com.tvd12.ezyfoxserver.support.asm.EzyExceptionHandlerImplementer)1 EzyUserRequestException (com.tvd12.ezyfoxserver.support.exception.EzyUserRequestException)1 EzyUserRequestHandler (com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler)1 HashMap (java.util.HashMap)1 ClassPool (javassist.ClassPool)1 CtClass (javassist.CtClass)1 Test (org.testng.annotations.Test)1