Skip to content

refactor: make the onFailure to be typed when we use onFailure(RuntimeException.class) #1848

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

hadadil
Copy link

@hadadil hadadil commented Apr 8, 2025

When I am using mutiny many times I would like to handle a given type of Exception nicelly to recover or convert the exception.

onFailure(SomeException.class)
   .transform(someException -> {
        LOGGER.info("some exception occured with field: {}", someException.someSpecialField);
        return new SomeElseExecption(someException.someSpecialField, someException);
    });

in this case the someException is type of SomeException so we can operate on this exception type safe and not as Throwable anymore.
in the past I have to do something like this

onFailure(SomeException.class)
   .transform(throwable -> {
        if (throwable instanceof SomeException someException) {
            LOGGER.info("some exception occured with field: {}", someException.someSpecialField);
            return new SomeElseExecption(someException.someSpecialField, someException);
        }
       // this is impossible, but I still need to do there something ?!
    });

@hadadil hadadil force-pushed the makeOnFailureToBeTyped branch from 802f2f1 to a430044 Compare April 8, 2025 13:29
Copy link

codecov bot commented Apr 8, 2025

Codecov Report

Attention: Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.

Project coverage is 88.90%. Comparing base (c55bb92) to head (7ac49be).
Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
...mallrye/mutiny/operators/uni/UniOnItemConsume.java 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #1848      +/-   ##
============================================
+ Coverage     88.89%   88.90%   +0.01%     
+ Complexity     3062     3058       -4     
============================================
  Files           409      409              
  Lines         13029    13033       +4     
  Branches       1642     1642              
============================================
+ Hits          11582    11587       +5     
+ Misses          821      817       -4     
- Partials        626      629       +3     
Files with missing lines Coverage Δ
...entation/src/main/java/io/smallrye/mutiny/Uni.java 90.90% <ø> (ø)
...n/java/io/smallrye/mutiny/groups/UniOnFailure.java 94.44% <100.00%> (+0.15%) ⬆️
...main/java/io/smallrye/mutiny/groups/UniOnItem.java 100.00% <ø> (ø)
...smallrye/mutiny/helpers/spies/UniOnFailureSpy.java 100.00% <ø> (ø)
...java/io/smallrye/mutiny/operators/AbstractUni.java 90.00% <100.00%> (ø)
...lrye/mutiny/operators/uni/UniOnFailureFlatMap.java 80.43% <100.00%> (+0.43%) ⬆️
...ye/mutiny/operators/uni/UniOnFailureTransform.java 93.33% <100.00%> (+0.22%) ⬆️
...mallrye/mutiny/operators/uni/UniOnItemConsume.java 89.18% <66.66%> (+0.30%) ⬆️

... and 9 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hadadil hadadil force-pushed the makeOnFailureToBeTyped branch from a430044 to 80110a1 Compare April 8, 2025 16:00
@jponge
Copy link
Member

jponge commented Apr 8, 2025

Not a review: I would use refactor!: xyz as a commit message, because it's a refactoring with breaking changes.

@hadadil hadadil force-pushed the makeOnFailureToBeTyped branch from 80110a1 to aefa34a Compare April 8, 2025 17:37
@hadadil
Copy link
Author

hadadil commented Apr 8, 2025

Not a review: I would use refactor!: xyz as a commit message, because it's a refactoring with breaking changes.

good point. updated.
hopefully as I just only changed internal stuff this still be fine to go with it, but let's see.

Copy link
Member

@jponge jponge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for looking into this, this is something we could have done better in the early days of Mutiny since onFailure with a guard should indeed have a more precise type in the next action.

That being said this introduces binary breaking changes, so I'd like to wait a bit before we decide to go ahead with this PR. This might land in a major release in a few months instead.

I will also ping colleagues for reviews / opinions.

*/
public class UniOnFailure<T> {
public class UniOnFailure<T, TT extends Throwable> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I would rename the TT parametric type to E (for error).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add more unit tests to show the failure type specialization in the API, especially with all the impacted groups / operators.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I addressed your comments and also I have added few tests to cover the touched methods.
let me know if you think I missed anything.

@jponge jponge requested review from ozangunalp and cescoffier April 16, 2025 07:42
@jponge
Copy link
Member

jponge commented Apr 16, 2025

@ozangunalp @cescoffier I've asked for review, but note that there is no rush for this PR. We should eventually consider it for Mutiny 3 I think due to the presence of binary breaking changes (source-level should be fine, but I've asked @hadadil to increase test coverage here so we see more of the new usage)

@hadadil hadadil requested a review from jponge April 17, 2025 13:16
Copy link
Contributor

@cescoffier cescoffier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, and yes, we should have done that since day 1 (my fault).

@hadadil hadadil force-pushed the makeOnFailureToBeTyped branch from 50656fb to 695b604 Compare April 22, 2025 09:29
@prakashelango
Copy link

Do we have any road map for mutiny ex: 3.x? similar to quarkus wanna see this breaking change soon.

@hadadil hadadil force-pushed the makeOnFailureToBeTyped branch from 695b604 to 7ac49be Compare April 24, 2025 13:49
@hadadil hadadil changed the title make the onFailure to be typed when we use onFailure(RuntimeException.class) refactor: make the onFailure to be typed when we use onFailure(RuntimeException.class) Apr 25, 2025
@jponge
Copy link
Member

jponge commented Apr 30, 2025

We don't have roadmap yet, but we'll sort it by mid-June I think.

Meanwhile you can always apply the patch on your own build of Mutiny (not ideal I know, but...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants