If statement SpringReactor:当publisher发出值时如何引发异常?

If statement SpringReactor:当publisher发出值时如何引发异常?,if-statement,exception,switch-statement,project-reactor,If Statement,Exception,Switch Statement,Project Reactor,我正在学习Reactor的反应式编程,我想实现一个注册场景,用户可以将多个帐户分配给同一个配置文件。但是,分配给个人资料的用户名和分配给帐户的电话必须是唯一的 正如您在下面的代码片段中所看到的,如果Reactor提供操作符switchIfNotEmpty,那么这个场景将很容易实现 public Mono<PersonalAccountResponse> createPersonalAccount(PersonalAccountRequest request) { return

我正在学习Reactor的反应式编程,我想实现一个注册场景,用户可以将多个帐户分配给同一个配置文件。但是,分配给个人资料的用户名和分配给帐户的电话必须是唯一的

正如您在下面的代码片段中所看到的,如果Reactor提供操作符
switchIfNotEmpty
,那么这个场景将很容易实现

public Mono<PersonalAccountResponse> createPersonalAccount(PersonalAccountRequest request) {
    return Mono
            .just(request.isAlreadyUser())
            .flatMap(isAlreadyUser ->  {
                if(isAlreadyUser){
                    return profileDao
                            .findByUsername(request.getUsername()) //
                            .switchIfEmpty(Mono.error(() -> new IllegalArgumentException("...")));
                }else{
                    return profileDao
                            .findByUsername(request.getUsername())
                            .switchIfEmpty(Mono.from(profileDao.save(profileData)))
                            .switchIfNotEmpty(Mono.error(() -> new IllegalArgumentException("...")));
                }
            })
            .map(profileData -> personalAccountMapper.toData(request))
            .flatMap(accountData -> personalAccountDao
                                            .retrieveByMobile(request.getMobileNumber())
                                            .switchIfEmpty(Mono.from(personalAccountDao.save(accountData)))
                                            .switchIfNotEmpty(Mono.error(() -> new IllegalArgumentException("..."))))
            .map(data ->  personalAccountMapper.toResponse(data, request.getUsername()));
}
public Mono createPersonalAccount(PersonalAccountRequest请求){
返回单声道
.just(request.isAlreadyUser())
.flatMap(isAlreadyUser->{
如果(isAlreadyUser){
返回配置文件DAO
.findByUsername(请求.getUsername())//
.switchIfEmpty(Mono.error(()->new IllegalArgumentException(“…”));
}否则{
返回配置文件DAO
.findByUsername(请求.getUsername())
.switchIfEmpty(Mono.from(profileDao.save(profileData)))
.switchIfNotEmpty(Mono.error(()->new IllegalArgumentException(“…”);
}
})
.map(profileData->personalAccountMapper.toData(请求))
.flatMap(accountData->personalAccountDao
.retrieveByMobile(请求.GetMobileEnumber())
.switchIfEmpty(Mono.from(personalAccountDao.save(accountData)))
.switchIfNotEmpty(Mono.error(()->new IllegalArgumentException(“…”))
.map(数据->personalAccountMapper.toResponse(数据,request.getUsername());
}
如果没有
开关ifnotempty
,我如何实现此要求


感谢

要在发布者发出值时传播异常,可以使用对发出的值进行操作的多个运算符之一

一些例子:

fluxOrMono.flatMap(下一步->Mono.error(新的IllegalArgumentException())
fluxOrMono.map(下一步->{抛出新的IllegalArgumentException();})
fluxOrMono.doOnNext(下一步->{抛出新的IllegalArgumentException();})
fluxOrMono.handle((下一步,sink)->sink.error(新的IllegalArgumentException())

不要忘记
concatMap