Search in sources :

Example 1 with CommandActionExecutionException

use of com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException in project Hystrix by Netflix.

the class AbstractHystrixCommand method process.

/**
 * Executes an action. If an action has failed and an exception is ignorable then propagate it as HystrixBadRequestException
 * otherwise propagate original exception to trigger fallback method.
 * Note: If an exception occurred in a command directly extends {@link java.lang.Throwable} then this exception cannot be re-thrown
 * as original exception because HystrixCommand.run() allows throw subclasses of {@link java.lang.Exception}.
 * Thus we need to wrap cause in RuntimeException, anyway in this case the fallback logic will be triggered.
 *
 * @param action the action
 * @return result of command action execution
 */
Object process(Action action) throws Exception {
    Object result;
    try {
        result = action.execute();
        flushCache();
    } catch (CommandActionExecutionException throwable) {
        Throwable cause = throwable.getCause();
        if (isIgnorable(cause)) {
            throw new HystrixBadRequestException(cause.getMessage(), cause);
        }
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        } else if (cause instanceof Exception) {
            throw (Exception) cause;
        } else {
            // instance of Throwable
            throw new CommandActionExecutionException(cause);
        }
    }
    return result;
}
Also used : HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) CommandActionExecutionException(com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) CommandActionExecutionException(com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException)

Example 2 with CommandActionExecutionException

use of com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException in project Hystrix by Netflix.

the class GenericObservableCommand method resumeWithFallback.

/**
 *{@inheritDoc}.
 */
@Override
protected Observable resumeWithFallback() {
    if (commandActions.hasFallbackAction()) {
        MetaHolder metaHolder = commandActions.getFallbackAction().getMetaHolder();
        Throwable cause = getExecutionException();
        if (cause instanceof CommandActionExecutionException) {
            cause = cause.getCause();
        }
        Object[] args = createArgsForFallback(metaHolder, cause);
        try {
            Object res = commandActions.getFallbackAction().executeWithArgs(executionType, args);
            if (res instanceof Observable) {
                return (Observable) res;
            } else if (res instanceof Single) {
                return ((Single) res).toObservable();
            } else if (res instanceof Completable) {
                return ((Completable) res).toObservable();
            } else {
                return Observable.just(res);
            }
        } catch (Exception e) {
            LOGGER.error(AbstractHystrixCommand.FallbackErrorMessageBuilder.create().append(commandActions.getFallbackAction(), e).build());
            throw new FallbackInvocationException(e.getCause());
        }
    }
    return super.resumeWithFallback();
}
Also used : Completable(rx.Completable) CommandActionExecutionException(com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException) FallbackInvocationException(com.netflix.hystrix.contrib.javanica.exception.FallbackInvocationException) Single(rx.Single) Observable(rx.Observable) CommandActionExecutionException(com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException) FallbackInvocationException(com.netflix.hystrix.contrib.javanica.exception.FallbackInvocationException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException)

Example 3 with CommandActionExecutionException

use of com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException in project Hystrix by Netflix.

the class GenericObservableCommand method construct.

/**
 *{@inheritDoc}.
 */
@Override
protected Observable construct() {
    Observable result;
    try {
        Observable observable = toObservable(commandActions.getCommandAction().execute(executionType));
        result = observable.onErrorResumeNext(new Func1<Throwable, Observable>() {

            @Override
            public Observable call(Throwable throwable) {
                if (isIgnorable(throwable)) {
                    return Observable.error(new HystrixBadRequestException(throwable.getMessage(), throwable));
                }
                return Observable.error(throwable);
            }
        });
        flushCache();
    } catch (CommandActionExecutionException throwable) {
        Throwable cause = throwable.getCause();
        if (isIgnorable(cause)) {
            throw new HystrixBadRequestException(cause.getMessage(), cause);
        }
        throw throwable;
    }
    return result;
}
Also used : HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) CommandActionExecutionException(com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException) Func1(rx.functions.Func1) Observable(rx.Observable)

Aggregations

CommandActionExecutionException (com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException)3 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)3 Observable (rx.Observable)2 FallbackInvocationException (com.netflix.hystrix.contrib.javanica.exception.FallbackInvocationException)1 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)1 Completable (rx.Completable)1 Single (rx.Single)1 Func1 (rx.functions.Func1)1