Netflix/Hystrix · error · RuntimeException

return type of '{}' method should be {};

Error message

return type of '{}' method should be {};

What it means

Javanica supports defining a command inline via a closure-like anonymous class returned from a @HystrixCommand-adjacent method; AbstractClosureFactory.createClosure verifies the anonymous object implements/extends the expected closure command type (e.g. HystrixCommand or HystrixObservableCommand) before reflecting out its run()/construct() method (INVOKE_METHOD). If the object's type does not match getClosureCommandType(), a RuntimeException with the formatted ERROR_TYPE_MESSAGE ('return type of ... method should be ...') is thrown, naming the outer method and the required type.

Source

Thrown at hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/closure/AbstractClosureFactory.java:65

            return createClosure(method.getName(), closureObj);
        } catch (InvocationTargetException e) {
            throw Throwables.propagate(e.getCause());
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }

    /**
     * Creates closure.
     *
     * @param rootMethodName the name of external method within which closure is created.
     * @param closureObj     the instance of specific anonymous class
     * @return new {@link Closure} instance
     * @throws Exception
     */
    Closure createClosure(String rootMethodName, final Object closureObj) throws Exception {
        if (!isClosureCommand(closureObj)) {
            throw new RuntimeException(format(ERROR_TYPE_MESSAGE, rootMethodName,
                    getClosureCommandType().getName()).getMessage());
        }
        Method closureMethod = closureObj.getClass().getMethod(INVOKE_METHOD);
        return new Closure(closureMethod, closureObj);
    }

    /**
     * Checks that closureObj is instance of necessary class.
     *
     * @param closureObj the instance of an anonymous class
     * @return true of closureObj has expected type, otherwise - false
     */
    abstract boolean isClosureCommand(final Object closureObj);

    /**
     * Gets type of expected closure type.
     *
     * @return closure (anonymous class) type

View on GitHub (pinned to 5ce3bc58c3)

Solutions

  1. Read the message: it names the outer method and the exact class the closure must extend/implement
  2. Make the anonymous class returned from that method extend the required type (HystrixCommand for sync/async, HystrixObservableCommand for observable)
  3. Ensure the anonymous class overrides the required method (run() or construct())
  4. If you did not intend closure-style commands, switch to plain annotation style and remove the anonymous class return

Example fix

// before
public Object getUserClosure() {
    return new Callable<User>() { public User call() { ... } };
}

// after
public HystrixCommand<User> getUserClosure() {
    return new HystrixCommand<User>(setter) { protected User run() { ... } };
}
Defensive patterns

Strategy: type-guard

Type guard

boolean isValidClosure(Object closureObj, Class<?> expected) {
    return closureObj != null && expected.isAssignableFrom(closureObj.getClass());
}
// before returning: if (!isValidClosure(obj, HystrixCommand.class)) throw new IllegalStateException("closure must be HystrixCommand");

Try / catch

catch (RuntimeException e) { if (e.getMessage().contains("should be")) throw new IllegalStateException("Closure signature error — check outer method return type", e); throw e; }

Prevention

When it happens

Trigger: Returning an anonymous class from a closure-style method that is not a subclass of the expected Hystrix command type — e.g. returning `new HystrixObservableCommand<String>(...) {...}` where the factory expects HystrixCommand, or returning an arbitrary Callable/Object.

Common situations: Refactoring a closure command from sync to observable (or vice versa) without changing the enclosing method's declared return type; mixing closure-style and annotation-style usage; upgrading Javanica versions where closure support semantics tightened.

Related errors


AI-assisted analysis of Netflix/Hystrix@5ce3bc58c3 (2026-08-14). Data as JSON: /api/errors/d601be3273c91702. Report an issue: GitHub.