2

I have a service layer interface that looks like this:

public interface MyService {
    void save(DomainClass domainObject) throws MyServiceException;
}

I use Hystrix to protect the implementation method:

public class MyServiceImpl implements MyService {
    @HystrixCommand
    public void save(DomainClass domainObject) throws MyServiceException {
        remoteServiceClient.persist(domainObject);
    }
}

When the remoteServiceClient fails or times out, Hystrix throws HystrixRuntimeException. But I do not want the clients of the service to see any Hystrix related exceptions (they should be oblivious to the implementation details of the service, right?). I want to throw my MyServiceException checked exception. Is it possible to do that? Should I structure my implementation in a different way?

Floern
  • 33,559
  • 24
  • 104
  • 119
Mohamed
  • 87
  • 1
  • 9

3 Answers3

0

Someone asked similar question here: Get failure exception in @HystrixCommand fallback method

So your case could be implemented as:

public class MyServiceImpl implements MyService {

    @HystrixCommand(fallbackMethod = "saveFallback")
    public void save(DomainClass domainObject) throws MyServiceException {
        remoteServiceClient.persist(domainObject);
    }

    @HystrixCommand
    void saveFallback(Throwable e) {
        throw new MyServiceException();
    }
}
Community
  • 1
  • 1
Alex.Bai
  • 196
  • 9
0

Use hystrix-javanica 1.5.7

The error propagation has been fine tuned to a greater extent in this and the exceptions are not wrapped as HystrixRuntimeException by default.

Updated error propagation:

If you have implemented the fallback method and even that fails, then the exception thrown by fallback method would be propagated to the user

//FallbackException and CommandException are user defined
@HystrixCommand(fallbackMethod = "fall")
public String getInfo(boolean fail) {
    throw new CommandException("Command failed....falling back");       
}

public String fall(boolean fail) {  
       throw new FallbackException("Fallback failed....");      
}

Here in this case FallbackException would be thrown.

Dharmvir Tiwari
  • 886
  • 3
  • 12
  • 25
0

Dharmvir, this is not what I'm seeing. Following code excerpt always wraps any error in the HystrixRuntimeException. I've had to interrogate that object and retrieve the actual error from the fallback as below. It doesn't sit well with me the way I've had to work around this:

@Override
@HystrixCommand(
        commandKey = "gateway",
        threadPoolKey = "gateway",
        fallbackMethod = "fallback")
public void process(final TxnLog mTxn, final Item item) {

...force timeout
}

    public void fallback(final TxnLog mTxn, final Item item, final Throwable cause) {

    ...other code

        if (cause != null && cause instanceof HystrixTimeoutException) {

           throw new InHouseException(ErrorInfoFactory.externalGatewayTimeout("External system communication timeout"));
        }
    }

    } catch (HystrixRuntimeException e) {

        if (e.getFallbackException().getCause() instanceof InHouseException) {

            InHouseException c = ((InHouseException) e.getFallbackException().getCause());

            ...return actual error
Vicky Kapadia
  • 6,025
  • 2
  • 24
  • 30
sapatos
  • 1,292
  • 3
  • 21
  • 44