'Raise error / assert in webflux sequence?

Let's say I have the following webflux snippet:

.map(str -> str.split(","))
.OnErrorResume(...)

I want to make sure that split returns an array of EXACTLY x items. Otherwise I want to go into the OnErrorResume. Is there a webflux-y way to do that? filter will just remove the bad items, but that isn't what I want.

Do I need to expand the map to something like:

{
  String[] arr = str.split(",");
  if (arr.length != 3)
     return Mono.error();
  return arr;
}

Or is there something built in?



Solution 1:[1]

Did you try handle method?

.map(str -> str.split(","))
.<String[]>handle((arr, sink) -> {
    if (arr.length == x)
         sink.next(arr);
    else
         sink.error(new ArrayLengthException());
   })
.onErrorResume(err -> Mono.just(...));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1