Home > other >  Raise error / assert in webflux sequence?
Raise error / assert in webflux sequence?

Time:05-04

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?

CodePudding user response:

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(...));
  • Related