Search in sources :

Example 1 with ExceptionMatch

use of io.seata.saga.statelang.domain.TaskState.ExceptionMatch in project seata by seata.

the class EngineUtils method handleException.

/**
 * Handle exceptions while ServiceTask or ScriptTask Executing
 *
 * @param context
 * @param state
 * @param e
 */
public static void handleException(ProcessContext context, AbstractTaskState state, Throwable e) {
    List<ExceptionMatch> catches = state.getCatches();
    if (CollectionUtils.isNotEmpty(catches)) {
        for (TaskState.ExceptionMatch exceptionMatch : catches) {
            List<String> exceptions = exceptionMatch.getExceptions();
            List<Class<? extends Exception>> exceptionClasses = exceptionMatch.getExceptionClasses();
            if (CollectionUtils.isNotEmpty(exceptions)) {
                if (exceptionClasses == null) {
                    synchronized (exceptionMatch) {
                        exceptionClasses = exceptionMatch.getExceptionClasses();
                        if (exceptionClasses == null) {
                            exceptionClasses = new ArrayList<>(exceptions.size());
                            for (String expStr : exceptions) {
                                Class<? extends Exception> expClass = null;
                                try {
                                    expClass = (Class<? extends Exception>) ScriptTaskStateHandler.class.getClassLoader().loadClass(expStr);
                                } catch (Exception e1) {
                                    LOGGER.warn("Cannot Load Exception Class by getClass().getClassLoader()", e1);
                                    try {
                                        expClass = (Class<? extends Exception>) Thread.currentThread().getContextClassLoader().loadClass(expStr);
                                    } catch (Exception e2) {
                                        LOGGER.warn("Cannot Load Exception Class by Thread.currentThread()" + ".getContextClassLoader()", e2);
                                    }
                                }
                                if (expClass != null) {
                                    exceptionClasses.add(expClass);
                                }
                            }
                            exceptionMatch.setExceptionClasses(exceptionClasses);
                        }
                    }
                }
                for (Class<? extends Exception> expClass : exceptionClasses) {
                    if (expClass.isAssignableFrom(e.getClass())) {
                        ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_CURRENT_EXCEPTION_ROUTE, exceptionMatch.getNext());
                        return;
                    }
                }
            }
        }
    }
    LOGGER.error("Task execution failed and no catches configured");
    ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_IS_EXCEPTION_NOT_CATCH, true);
}
Also used : ExceptionMatch(io.seata.saga.statelang.domain.TaskState.ExceptionMatch) HierarchicalProcessContext(io.seata.saga.proctrl.HierarchicalProcessContext) ScriptTaskStateHandler(io.seata.saga.engine.pcext.handlers.ScriptTaskStateHandler) TaskState(io.seata.saga.statelang.domain.TaskState) AbstractTaskState(io.seata.saga.statelang.domain.impl.AbstractTaskState) ExceptionMatch(io.seata.saga.statelang.domain.TaskState.ExceptionMatch)

Example 2 with ExceptionMatch

use of io.seata.saga.statelang.domain.TaskState.ExceptionMatch in project seata by seata.

the class AbstractTaskStateParser method parseCatch.

protected List<ExceptionMatch> parseCatch(List<Object> catchList) {
    List<ExceptionMatch> exceptionMatchList = new ArrayList<>(catchList.size());
    for (Object exceptionMatchObj : catchList) {
        Map<String, Object> exceptionMatchMap = (Map<String, Object>) exceptionMatchObj;
        ExceptionMatchImpl exceptionMatch = new ExceptionMatchImpl();
        exceptionMatch.setExceptions((List<String>) exceptionMatchMap.get("Exceptions"));
        exceptionMatch.setNext((String) exceptionMatchMap.get("Next"));
        exceptionMatchList.add(exceptionMatch);
    }
    return exceptionMatchList;
}
Also used : ExceptionMatch(io.seata.saga.statelang.domain.TaskState.ExceptionMatch) ArrayList(java.util.ArrayList) ExceptionMatchImpl(io.seata.saga.statelang.domain.impl.AbstractTaskState.ExceptionMatchImpl) Map(java.util.Map)

Aggregations

ExceptionMatch (io.seata.saga.statelang.domain.TaskState.ExceptionMatch)2 ScriptTaskStateHandler (io.seata.saga.engine.pcext.handlers.ScriptTaskStateHandler)1 HierarchicalProcessContext (io.seata.saga.proctrl.HierarchicalProcessContext)1 TaskState (io.seata.saga.statelang.domain.TaskState)1 AbstractTaskState (io.seata.saga.statelang.domain.impl.AbstractTaskState)1 ExceptionMatchImpl (io.seata.saga.statelang.domain.impl.AbstractTaskState.ExceptionMatchImpl)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1