Home > Software engineering >  return Mono.error inside Map Spring Webflux
return Mono.error inside Map Spring Webflux

Time:03-04

For Matching condition, return Mono.error from code , but gives compilation error. I commented out //return Mono.error(new RuntimeException("User Phone Exists already"));

Compilation Error: Required Tyoe Mono, Provided Mono

public Mono getEventSlotBookWithAppointmentId(EventSlotBook eventSlotBookEntity) {

    Query query = new Query();
    query.addCriteria(
            new Criteria().andOperator(
                    Criteria.where("eventId").is(eventSlotBookEntity.getEventId()),
                    Criteria.where("eventConfigId").is(eventSlotBookEntity.getEventConfigId()),
                    Criteria.where("eventSlotId").is(eventSlotBookEntity.getEventSlotId())));

    return this.reactiveMongoTemplate.findOne(query, EventSlotBook.class)
            .map(eventSlotBookEntityFromDb -> {
                EventSlotBook eventSlotNewEntity = eventSlotBookEntityFromDb.toBuilder().build();

                if(eventSlotNewEntity.getEventUsers() != null) {
                    for(EventUser eventUserIno:eventSlotNewEntity.getEventUsers()) {
                        if(eventUserIno.getPhoneNumber().equalsIgnoreCase(eventSlotBookEntity.getEventUser().getPhoneNumber())){
                            //return Mono.error(new RuntimeException("User Phone Exists already"));
                        }
                    }
                }


                int maxTokenVal = Integer.valueOf(eventSlotNewEntity.getMaxTokenVal()).intValue()   1;
                EventUser eventUser = new EventUser(eventSlotNewEntity.getEventUser().getName(),eventSlotNewEntity.getEventUser().getPhoneNumber(),String.valueOf(maxTokenVal));
                eventSlotNewEntity.getEventUsers().add(eventUser);
                eventSlotNewEntity.setMaxTokenVal(String.valueOf(maxTokenVal));
                eventSlotNewEntity.setEventUser(eventUser);


                return eventSlotNewEntity;
                //return Mono.error(new RuntimeException("Ts"));
            })
            .switchIfEmpty(getEventSlotBook(eventSlotBookEntity));
}

caller of Method : I should handle Mono.error and return to rest API that user already exists ?. Please help on this

public Mono<EventSlotBookRequestDto> saveEventSlotBook(Mono<EventSlotBookRequestDto> eventSlotBookRequestDtoMono){
    log.info("Start::SaveEventSlotBook");
    Mono<EventSlotBookRequestDto> eventDtoSaved =
            eventSlotBookRequestDtoMono.map(AppUtils::dtoToEntity)
                    .flatMap(eventSlotEntity -> getEventSlotBookWithAppointmentId(eventSlotEntity))
                    .doOnNext(eventSlotEntityBeforeSave -> {
                        log.info("@@@@BeforeSave::{}",eventSlotEntityBeforeSave);
                    })
                    .flatMap(eventSlotBookrepository::save)
                    .doOnNext( eventSlotBookAfterSave -> {
                        log.info("@@@@AfterSave::{}",eventSlotBookAfterSave);
                    })
                    .map(AppUtils::entityToDto);
    log.info("End::SaveEventSlotBook");
    return eventDtoSaved;
}

CodePudding user response:

map is used to apply a synchronous function to each item, therefore you can't return Mono from it. To return an error from map you could just throw an exception and error signal will be emited. As an alternative you you use handle operator and use SynchronousSink to emit next or error signal.

But in your case you need to use flatMap instead because saveEventSlotBook returns Mono and should be transformed asynchronously.

  • Related