What's the proper way to execute some logic in reactor if I'm expecting empty values?

crimson589

Suppose I have a Mono<MyObject> which would be populated by retrieving something from the database or another API. If I got no results from the database or the API returned with an empty body or 404 then I want to execute some logic.

Would I just create a Mono.error() and handle it like this

Mono<MyObject> myObjectMono = Mono.error(new NotFoundException("Error"));

myObject.doOnError(e -> {
    if (e instanceof NotFoundException) {
        //my logic for responses when database returns 0 rows or api response is empty/404
    }
    else {
        //other logic here for actual errors
    }
}).subscribe(myObject -> {
    //my logic when there's a value
});

Or do I use Mono.empty()? how do I handle empty though? I know there's switchIfEmpty() but I don't really have anything to return and don't want the subscribe logic to execute.

Mono<MyObject> myObjectMono = Mono.empty();

myObject.subscribe(myObject -> {
    //my logic when there's a value
});

Note that the starting point of this is not Http request so there's no client that I will return the Mono to, for simplicity let's just say it's a scheduled job.

Yevhenii Semenov

Both approaches are technically correct. The direction to take depends on the conventions established in your project. I'd suggest avoiding exceptions if possible because creating exceptions is a heavy operation because of its stack trace.

If you want to go with Mono.empty you can do it like this:

Mono.<String>empty()
    .switchIfEmpty(Mono.fromCallable(() -> {
        System.out.println("no response");
        return null;
    }))
    .subscribe(response -> {
        System.out.println("response = " + response);
    });

If you want to go with exceptions, you can do it like this:

Mono.<String>error(new RuntimeException(""))
    .subscribe(
        response -> {
            System.out.println("response = " + response);
        },
        error -> {
            System.out.println("error = " + error);
        }
    );

or

Mono.<String>error(new RuntimeException(""))
    .doOnError(NotFoundException.class, e -> {
        System.out.println("not found exception: " + e);
    })
    .doOnError(e -> {
        System.out.println("another exception: " + e);
    })
    .subscribe(response -> {
        System.out.println("response = " + response);
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I use Reactor's StepVerifier to verify a Mono is empty?

What is the efficient/proper way to flow multiple objects in reactor

What's the proper way of passing a ref to a prop?

What's the proper way to document callbacks with jsdoc?

What is the proper way to check for null values?

What is the proper way to test if a parameter is empty in a batch file?

What's the proper way to propagate .catch in promise?

What's the proper query for this. (needs orderby, make some of the values be last and more) MYSQL

What's the proper way to setup an Android PreferenceFragment?

What's the proper way to check if a constant is defined?

What's the proper way of Naming a class?

What's the best way to insert values into their "proper" place in a pandas dataframe by some (index) parameter?

What's the proper way to reraise a custom exception?

I was just expecting the char values to be printed.What is happening?

What's the reason for not getting the variable I'm expecting in the functional component?

What's the proper way to write a haskell function

What's the proper way of changing the color of SetDlgItemText?

What's the proper way to recurse in LLVM assembly?

What's the best way to show and hide parts of an MVC view based on some logic?

What's the proper way to make the AppBar fixed?

Using cin.get() doesn't seem to read the character I'm expecting. What's wrong?

What's the proper way to link to a route with parameters?

What's the proper way to share the interface?

What's the proper way to write hex literal values in Delphi?

What is the proper way to create a class instance with an empty dictionary and/or list?

What's the proper way to use Coroutines in Activity?

Getting Z values when expecting a proper output

not giving output the way I’m expecting

What's the canonical way of disallowing empty strings as values?