/** * Retrieves cause exception and wraps to {@link CommandActionExecutionException}. * * @param throwable the throwable */ private void propagateCause(Throwable throwable) throws CommandActionExecutionException { ExceptionUtils.propagateCause(throwable); }
/** * Wraps cause exception to {@link CommandActionExecutionException}. * * @param throwable the throwable */ public static CommandActionExecutionException wrapCause(Throwable throwable) { return new CommandActionExecutionException(throwable.getCause()); }
private Object getPropertyValue(String name, Object obj) throws HystrixCacheKeyGenerationException { try { return new PropertyDescriptor(name, obj.getClass()) .getReadMethod().invoke(obj); } catch (IllegalAccessException e) { throw new HystrixCacheKeyGenerationException(e); } catch (IntrospectionException e) { throw new HystrixCacheKeyGenerationException(e); } catch (InvocationTargetException e) { throw new HystrixCacheKeyGenerationException(e); } }
@Override @SuppressWarnings("unchecked") protected List<Object> getFallback() { if (getFallbackAction() != null) { final CommandAction commandAction = getFallbackAction(); try { return (List<Object>) process(new Action() { @Override Object execute() { MetaHolder metaHolder = commandAction.getMetaHolder(); Object[] args = toArgs(getCollapsedRequests()); args = createArgsForFallback(args, metaHolder, getExecutionException()); return commandAction.executeWithArgs(commandAction.getMetaHolder().getFallbackExecutionType(), args); } }); } catch (Throwable e) { LOGGER.error(FallbackErrorMessageBuilder.create() .append(commandAction, e).build()); throw new FallbackInvocationException(unwrapCause(e)); } } else { return super.getFallback(); } }
private void validateCompletableReturnType(Method commandMethod, Class<?> callbackReturnType) { if (Void.TYPE == callbackReturnType) { throw new FallbackDefinitionException(createErrorMsg(commandMethod, method, "fallback cannot return 'void' if command return type is " + Completable.class.getSimpleName())); } }
flushCache(); } catch (CommandActionExecutionException throwable) { Throwable cause = throwable.getCause(); if (isIgnorable(cause)) { throw new HystrixBadRequestException(cause.getMessage(), cause); } else { throw new CommandActionExecutionException(cause);
public HystrixObservableCommand.Setter buildObservableCommandSetter() { HystrixObservableCommand.Setter setter = HystrixObservableCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); try { setter.andCommandPropertiesDefaults(HystrixPropertiesManager.initializeCommandProperties(commandProperties)); } catch (IllegalArgumentException e) { throw new HystrixPropertyException("Failed to set Command properties. " + getInfo(), e); } return setter; }
private Throwable getCause(HystrixRuntimeException e) { if (e.getFailureType() != HystrixRuntimeException.FailureType.COMMAND_EXCEPTION) { return e; } Throwable cause = e.getCause(); // latest exception in flow should be propagated to end user if (e.getFallbackException() instanceof FallbackInvocationException) { cause = e.getFallbackException().getCause(); if (cause instanceof HystrixRuntimeException) { cause = getCause((HystrixRuntimeException) cause); } } else if (cause instanceof CommandActionExecutionException) { // this situation is possible only if a callee throws an exception which type extends Throwable directly CommandActionExecutionException commandActionExecutionException = (CommandActionExecutionException) cause; cause = commandActionExecutionException.getCause(); } return Optional.fromNullable(cause).or(e); }
private static MethodExecutionAction createCacheKeyAction(String method, MetaHolder metaHolder) { MethodExecutionAction cacheKeyAction = null; if (StringUtils.isNotBlank(method)) { Method cacheKeyMethod = getDeclaredMethod(metaHolder.getObj().getClass(), method, metaHolder.getMethod().getParameterTypes()); if (cacheKeyMethod == null) { throw new HystrixCachingException("method with name '" + method + "' doesn't exist in class '" + metaHolder.getObj().getClass() + "'"); } if (!cacheKeyMethod.getReturnType().equals(String.class)) { throw new HystrixCachingException("return type of cacheKey method must be String. Method: '" + method + "', Class: '" + metaHolder.getObj().getClass() + "'"); } MetaHolder cMetaHolder = MetaHolder.builder().obj(metaHolder.getObj()).method(cacheKeyMethod).args(metaHolder.getArgs()).build(); cacheKeyAction = new MethodExecutionAction(cMetaHolder.getObj(), cacheKeyMethod, cMetaHolder.getArgs(), cMetaHolder); } return cacheKeyAction; }
LOGGER.error(AbstractHystrixCommand.FallbackErrorMessageBuilder.create() .append(commandActions.getFallbackAction(), e).build()); throw new FallbackInvocationException(e.getCause());
LOGGER.error(FallbackErrorMessageBuilder.create() .append(commandAction, e).build()); throw new FallbackInvocationException(unwrapCause(e));
private void validatePlainReturnType(Class<?> commandReturnType, Class<?> fallbackReturnType, Method commandMethod, Method fallbackMethod) { if (!commandReturnType.isAssignableFrom(fallbackReturnType)) { throw new FallbackDefinitionException(createErrorMsg(commandMethod, fallbackMethod, "Fallback method '" + fallbackMethod + "' must return: " + commandReturnType + " or its subclass")); } }
/** * Retrieves cause exception and wraps to {@link CommandActionExecutionException}. * * @param throwable the throwable */ public static void propagateCause(Throwable throwable) throws CommandActionExecutionException { throw new CommandActionExecutionException(throwable.getCause()); }
public HystrixGeneratedCacheKey generateCacheKey(CacheInvocationContext<? extends Annotation> cacheInvocationContext) throws HystrixCacheKeyGenerationException { MethodExecutionAction cacheKeyMethod = cacheInvocationContext.getCacheKeyMethod(); if (cacheKeyMethod != null) { try { return new DefaultHystrixGeneratedCacheKey((String) cacheKeyMethod.execute(cacheInvocationContext.getExecutionType())); } catch (Throwable throwable) { throw new HystrixCacheKeyGenerationException(throwable); } } else { if (cacheInvocationContext.hasKeyParameters()) { StringBuilder cacheKeyBuilder = new StringBuilder(); for (CacheInvocationParameter parameter : cacheInvocationContext.getKeyParameters()) { CacheKey cacheKey = parameter.getCacheKeyAnnotation(); if (cacheKey != null && StringUtils.isNotBlank(cacheKey.value())) { appendPropertyValue(cacheKeyBuilder, Arrays.asList(StringUtils.split(cacheKey.value(), ".")), parameter.getValue()); } else { cacheKeyBuilder.append(parameter.getValue()); } } return new DefaultHystrixGeneratedCacheKey(cacheKeyBuilder.toString()); } else { return DefaultHystrixGeneratedCacheKey.EMPTY; } } }
/** * Creates instance of {@link HystrixCommand.Setter}. * * @return the instance of {@link HystrixCommand.Setter} */ public HystrixCommand.Setter build() throws HystrixPropertyException { HystrixCommand.Setter setter = HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); if (StringUtils.isNotBlank(threadPoolKey)) { setter.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(threadPoolKey)); } try { setter.andThreadPoolPropertiesDefaults(HystrixPropertiesManager.initializeThreadPoolProperties(threadPoolProperties)); } catch (IllegalArgumentException e) { throw new HystrixPropertyException("Failed to set Thread Pool properties. " + getInfo(), e); } try { setter.andCommandPropertiesDefaults(HystrixPropertiesManager.initializeCommandProperties(commandProperties)); } catch (IllegalArgumentException e) { throw new HystrixPropertyException("Failed to set Command properties. " + getInfo(), e); } return setter; }
/** *{@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; }
private void validateParametrizedType(Type commandReturnType, Type fallbackReturnType, Method commandMethod, Method fallbackMethod) { if (!commandReturnType.equals(fallbackReturnType)) { throw new FallbackDefinitionException(createErrorMsg(commandMethod, fallbackMethod, "Fallback method '" + fallbackMethod + "' must return: " + commandReturnType + " or its subclass")); } }
private FallbackMethod doFind(Class<?> enclosingType, Method commandMethod, boolean extended) { String name = getFallbackName(enclosingType, commandMethod); Class<?>[] fallbackParameterTypes = null; if (isDefault()) { fallbackParameterTypes = new Class[0]; } else { fallbackParameterTypes = commandMethod.getParameterTypes(); } if (extended && fallbackParameterTypes[fallbackParameterTypes.length - 1] == Throwable.class) { fallbackParameterTypes = ArrayUtils.remove(fallbackParameterTypes, fallbackParameterTypes.length - 1); } Class<?>[] extendedFallbackParameterTypes = Arrays.copyOf(fallbackParameterTypes, fallbackParameterTypes.length + 1); extendedFallbackParameterTypes[fallbackParameterTypes.length] = Throwable.class; Optional<Method> exFallbackMethod = getMethod(enclosingType, name, extendedFallbackParameterTypes); Optional<Method> fMethod = getMethod(enclosingType, name, fallbackParameterTypes); Method method = exFallbackMethod.or(fMethod).orNull(); if (method == null) { throw new FallbackDefinitionException("fallback method wasn't found: " + name + "(" + Arrays.toString(fallbackParameterTypes) + ")"); } return new FallbackMethod(method, exFallbackMethod.isPresent(), isDefault()); }
private void validateReturnType(Method commandMethod, Method fallbackMethod) { if (isGenericReturnType(commandMethod)) { List<Type> commandParametrizedTypes = flattenTypeVariables(commandMethod.getGenericReturnType()); List<Type> fallbackParametrizedTypes = flattenTypeVariables(fallbackMethod.getGenericReturnType()); Result result = equalsParametrizedTypes(commandParametrizedTypes, fallbackParametrizedTypes); if (!result.success) { List<String> msg = new ArrayList<String>(); for (Error error : result.errors) { Optional<Type> parentKindOpt = getParentKind(error.commandType, commandParametrizedTypes); String extraHint = ""; if (parentKindOpt.isPresent()) { Type parentKind = parentKindOpt.get(); if (isParametrizedType(parentKind)) { extraHint = "--> " + ((ParameterizedType) parentKind).getRawType().toString() + "<Ooops!>\n"; } } msg.add(String.format(error.reason + "\n" + extraHint + "Command type literal pos: %s; Fallback type literal pos: %s", positionAsString(error.commandType, commandParametrizedTypes), positionAsString(error.fallbackType, fallbackParametrizedTypes))); } throw new FallbackDefinitionException(createErrorMsg(commandMethod, method, StringUtils.join(msg, "\n"))); } } validatePlainReturnType(commandMethod, fallbackMethod); }
throw new FallbackDefinitionException(createErrorMsg(commandMethod, method, "fallback cannot return Future if the fallback isn't command when the command is async.")); throw new FallbackDefinitionException(createErrorMsg(commandMethod, method, "fallback cannot return Future if command isn't asynchronous.")); throw new FallbackDefinitionException(createErrorMsg(commandMethod, method, "fallback cannot return Observable if command isn't observable."));