Adds FallbackFactory, allowing access to the cause of a Hystrix fallback#443
Adds FallbackFactory, allowing access to the cause of a Hystrix fallback#443codefromthecrypt merged 1 commit intomasterfrom
Conversation
| return super.getFallback(); | ||
| } | ||
| try { | ||
| Object fallback = fallbackFactory.create(getFailedExecutionException()); |
There was a problem hiding this comment.
note: if all we want to do is log the cause, we can delete the entire pull request and just log here!
Throwable cause = getFailedExecutionException();
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "fallback due to: " + cause.getMessage(), cause);
}There was a problem hiding this comment.
No, I think folks want access to the actual exception object.
There was a problem hiding this comment.
okie dokie, then we don't need to delete this branch :)
7f4385f to
25d14da
Compare
|
PS for those who have existing fallback classes, here's the process for teaching them about the cause..
|
|
At first i was slightly confused by the name |
|
added a bunch of example tests, which aren't really about feign, more about a million ways to construct objects. One in particular could be helpful for those who want to retrofit an existing class and not declare a separate FallbackFactory. Note: if feign was java8 we wouldn't even need FallbackFactory (since we could use the Function type)! // retrofit so people don't have to track 2 classes
static class FallbackApiRetro implements Api, FallbackFactory<FallbackApiRetro> {
@Override public FallbackApiRetro create(Throwable cause) {
return new FallbackApiRetro(cause);
}
final Throwable cause; // nullable
public FallbackApiRetro() {
this(null);
}
FallbackApiRetro(Throwable cause) {
this.cause = cause;
}
// actual api methods can lazy implement any use of the cause.
@Override public String invoke() {
return cause != null ? cause.getMessage() : "foo";
}
} |
a364b23 to
4457aea
Compare
|
ps if we aren't quite ready to land this PR, we should probably go ahead and do a release. If that's the case, I can add a logging line as an interim step. Lemme know. |
4457aea to
1b71215
Compare
|
+1 I definitely have a need for this, right now when a fallback is invoked I have no idea about the underlying reason |
|
+1 this feature is a must |
The cause of the fallback is now logged by default to FINE level. You can programmatically inspect
the cause by making your own `FallbackFactory`. In many cases, the cause will be a `FeignException`,
which includes the http status.
Here's an example of using `FallbackFactory`:
```java
// This instance will be invoked if there are errors of any kind.
FallbackFactory<GitHub> fallbackFactory = cause -> (owner, repo) -> {
if (cause instanceof FeignException && ((FeignException) cause).status() == 403) {
return Collections.emptyList();
} else {
return Arrays.asList("yogi");
}
};
GitHub github = HystrixFeign.builder()
...
.target(GitHub.class, "https://api.github.com", fallbackFactory);
```
1b71215 to
b9e6149
Compare
|
OK will merge on green then cut feign 9.3 |
|
Hi is this Feign FallbackFactory is working? I am using version 9.3.1. I am still not able to get the cause from fallback. I used the code example from retrofit test case. |
|
@seetharamani there are tests that demonstrate it working. If you think you have found a bug, open an issue with steps to reproduce or a sample project that demonstrates the problem. Asking if it works on the merge PR isn't real helpful. |
|
|
Opened the issue with sample project |
…ack (#443) The cause of the fallback is now logged by default to FINE level. You can programmatically inspect the cause by making your own `FallbackFactory`. In many cases, the cause will be a `FeignException`, which includes the http status. Here's an example of using `FallbackFactory`: ```java // This instance will be invoked if there are errors of any kind. FallbackFactory<GitHub> fallbackFactory = cause -> (owner, repo) -> { if (cause instanceof FeignException && ((FeignException) cause).status() == 403) { return Collections.emptyList(); } else { return Arrays.asList("yogi"); } }; GitHub github = HystrixFeign.builder() ... .target(GitHub.class, "https://api.github.com", fallbackFactory); ```
…ack (#443) The cause of the fallback is now logged by default to FINE level. You can programmatically inspect the cause by making your own `FallbackFactory`. In many cases, the cause will be a `FeignException`, which includes the http status. Here's an example of using `FallbackFactory`: ```java // This instance will be invoked if there are errors of any kind. FallbackFactory<GitHub> fallbackFactory = cause -> (owner, repo) -> { if (cause instanceof FeignException && ((FeignException) cause).status() == 403) { return Collections.emptyList(); } else { return Arrays.asList("yogi"); } }; GitHub github = HystrixFeign.builder() ... .target(GitHub.class, "https://api.github.com", fallbackFactory); ```
The cause of the fallback is now logged by default to FINE level. You can programmatically inspect
the cause by making your own
FallbackFactory. In many cases, the cause will be aFeignException,which includes the http status.
Here's an example of using
FallbackFactory:Fixes #431
See #432