Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to ignore business exception in feign client with hystrix. #472

Closed
sumit4myself opened this issue Oct 4, 2016 · 9 comments
Closed
Labels
question General usage or 'how-to' questions

Comments

@sumit4myself
Copy link

How to ignore business exception in feign client with Hystrix.

@spencergibb spencergibb added the question General usage or 'how-to' questions label Oct 5, 2016
@spencergibb
Copy link
Contributor

@tjuchniewicz
Copy link
Contributor

@sumit4myself Here is described concept I currently use: https://github.com/spring-cloud/spring-cloud-netflix/issues/969#issuecomment-242131309

But this is for @FeignClient. It should work also for pure Feign.

@sumit4myself
Copy link
Author

@spencergibb @tjuchniewicz I don't want to write any fallbak for business exception as i need to show it to the end user. Eg. If authentication fails i need to show the message to end user.
but when i use hystrix with feign hystrix warp the my business exception with HystrixRuntimeException.

HytrixCommand accept list of exception to be ignored is there any configuration for fiegn also.

@tjuchniewicz
Copy link
Contributor

tjuchniewicz commented Oct 10, 2016

Feign uses network for transport. Exceptions are not propagated from service to client over network by default. For now the only way I found is to use concept like this:

  1. On service side when exception occurs return HTTP 500 and JSON with exception class name and message. Service code have be updated.
  2. On client side use custom ErrorDecoder which translates JSON error message to exception and throw it.

@sumit4myself
Copy link
Author

sumit4myself commented Oct 11, 2016

@tjuchniewicz i have implemented it in the same way. but problem is hystrix wrap my business exception with HystrixRuntimeException how can i ignore it, I dont want to off hystrix for fiegn client.

code :

@Component
public class ErrorResponseDecoder implements ErrorDecoder {

private ErrorDecoder delegate = new ErrorDecoder.Default();

private ObjectMapper mapper = new ObjectMapper();

@Override
public Exception decode(String methodKey, Response response) {
    HttpHeaders responseHeaders = new HttpHeaders();
    for (Map.Entry<String, Collection<String>> entry : response.headers().entrySet()) {
        responseHeaders.put(entry.getKey(), new ArrayList<>(entry.getValue()));
    }
    try {
        String body = Util.toString(response.body().asReader());
        Map<String, String> map = new HashMap<>();
        map = mapper.readValue(body, new TypeReference<Map<String, String>>() {
        });

        if (StringUtils.equals("com.....common.exception.BussinessException",
                map.get("exception"))) {
            return new BussinessException(map.get("code"), map.get("message"));

        } else if (StringUtils.equals("com......common.exception.SystemException",
                map.get("exception"))) {
            return new SystemException(map.get("code"), map.get("message"));
        }

    } catch (IOException e) {
        throw new SystemException(SystemErrorCode.SYSTEM_ERROR, e);
    }
    return delegate.decode(methodKey, response);
}

}

@tjuchniewicz
Copy link
Contributor

tjuchniewicz commented Oct 11, 2016

Code looks good. I have similar solution but I return HystrixBadRequestException to disable circuit-breaker logic for business exceptions. For this case I had to fix this: Netflix/Hystrix#1366. See our private branch: https://github.com/ebjwc/Hystrix/commit/75fa50d8a685824e5efafd529c98eaaeb9133493. Now I can catch my businnes exception instead of HystrixBadRequestException.

@sumit4myself
Copy link
Author

@tjuchniewicz thanks, When the updated code will be available ?

@tjuchniewicz
Copy link
Contributor

tjuchniewicz commented Oct 12, 2016

Not sure. I'm not a Netflix/Hystrix developer. Please vote for Netflix/Hystrix#1366!

@kdavisk6
Copy link
Member

kdavisk6 commented Mar 6, 2018

This appears to be quite old and going through the Hystrix projects, this situation should be able to be handled by using Hystrix directly. Also, the changes referenced above have been accepted into Hystrix core.

I suggest that this can now be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question General usage or 'how-to' questions
Projects
None yet
Development

No branches or pull requests

4 participants