question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CircuitBreaker.decorateSupplier catches Exception but Supplier can only throw RuntimeException

See original GitHub issue

Resilience4j version: 1.5.0

Java version: 1.11

Thanks for raising a Resilience4j issue.

I see that we catch a general Exception from supplier.get() & publish an Error event. But Supplier only throws RuntimeExceptions.

If the Supplier can only throw RuntimeException, why not catch RuntimeException and be explicit?

https://github.com/resilience4j/resilience4j/blob/7fe040e1bf852ccaaaa8317448a8e6fce0202373/resilience4j-circuitbreaker/src/main/java/io/github/resilience4j/circuitbreaker/CircuitBreaker.java#L201

    static <T> Supplier<T> decorateSupplier(CircuitBreaker circuitBreaker, Supplier<T> supplier) {
        return () -> {
            circuitBreaker.acquirePermission();
            final long start = circuitBreaker.getCurrentTimestamp();
            try {
                T result = supplier.get();
                long duration = circuitBreaker.getCurrentTimestamp() - start;
                circuitBreaker.onResult(duration, circuitBreaker.getTimestampUnit(), result);
                return result;
            } catch (Exception exception) {
                // Do not handle java.lang.Error
                long duration = circuitBreaker.getCurrentTimestamp() - start;
                circuitBreaker.onError(duration, circuitBreaker.getTimestampUnit(), exception);
                throw exception;
            }
        };
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
RobWincommented, Jan 22, 2021

Yes, you can use

circuitbreakerConfig.recordException(ex -> ex.getCause() instanceof YourException)
retryConfig.retryOnException(ex -> ex.getCause() instanceof YourException)
0reactions
asthinasthicommented, Jan 21, 2021

We have an internal ThrowingSupplier which is a wrapper around Supplier to allow checked exception in the lambda expressions. We sneakyThrow Exceptions as RuntimeExceptions.

We were using recordExceptions list & assuming that retry would work for Suppliers. 🤦 My Bad.

This is just an observation that we could catch only RuntimeExceptions because Suppliers can only throw RuntimeException.

@RobWin Is there a way to use recordExceptions to retry on cause instead of actual exception. We are wrapping our Checked Exceptions and throwing RuntimExceptions in our ThrowingSupplier

Is using CheckedFunction only way to make the Retry work on recordExceptions ?

Also, I can’t seem to find a Decorator for CheckedSupplier for ThreadpoolBulkhead. #1307

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resilience4j.retry with exception is not working - Stack Overflow
If you want to catch checked exceptions, you have to use decorateCallable .
Read more >
Examples - resilience4j
Decorate your call to BackendService.doSomething() with a CircuitBreaker and execute the decorated supplier and recover from any exception.
Read more >
Implementing Retry with Resilience4j - Reflectoring
This article is a deep dive into the Resilience4j retry module and shows why, when and how to use it to build resilient...
Read more >
Guide to Resilience4j - Baeldung
Learn how to use the most useful modules from the Resilience4j library to build resilient systems.
Read more >
CircuitBreaker.decorateSupplier - Java - Tabnine
public DecorateSupplier withCircuitBreaker(CircuitBreaker circuitBreaker) { supplier = CircuitBreaker.decorateSupplier(circuitBreaker, supplier);
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found