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) typeView on GitHub (pinned to 5ce3bc58c3)
Solutions
- Read the message: it names the outer method and the exact class the closure must extend/implement
- Make the anonymous class returned from that method extend the required type (HystrixCommand for sync/async, HystrixObservableCommand for observable)
- Ensure the anonymous class overrides the required method (run() or construct())
- 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
- Declare the closure-factory method's return type as the exact command class (HystrixCommand<T>/HystrixObservableCommand<T>) so the compiler catches mismatches
- Avoid closure-style commands unless required; prefer plain annotation style
- Add a unit test per closure method returning the built command
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
- Unsupported type {}
- method with name '{}' doesn't exist in class '{}'
- unsupported rx type: {}
- Incompatible return types. \nCommand method: " + commandMeth
- fallback method wasn't found: " + name + "(" + Arrays.toStri
AI-assisted analysis of Netflix/Hystrix@5ce3bc58c3 (2026-08-14).
Data as JSON: /api/errors/d601be3273c91702.
Report an issue: GitHub.