Пружинный реактор для возврата ошибки, если элемент не существует - PullRequest
0 голосов
/ 11 июля 2020

Как вы видите ниже, я пытаюсь добавить цену к продукту. Я хочу выдать ошибку, если товар уже существует. Однако приведенный ниже код не вызывает исключение priceInformationAlreadyExists, вместо этого он начинает выполнение операторов за пределами оператора if

   return productPresent.flatMap(isPresent -> {
        if(isPresent){
            log.info("ispresent {}", isPresent);
            Mono.error(() -> new PriceInformationAlreadyExists("Price Information not found for product" + productId));
        }
        log.info("isNotpresent {}", isPresent);
        Price price = Price.builder().productId(productId).build();
        BeanUtils.copyProperties(priceRequest, price);
        return priceRepository.save(price).map(p2 -> buildPriceResponse(p2));
    });

1 Ответ

2 голосов
/ 11 июля 2020

У вас отсутствует отчет о возврате.

return productPresent.flatMap(isPresent -> {
        if(isPresent){
            log.info("ispresent {}", isPresent);
            return Mono.error(new PriceInformationAlreadyExists("Price Information not found for product" + productId));
        }
        log.info("isNotpresent {}", isPresent);
        Price price = Price.builder().productId(productId).build();
        BeanUtils.copyProperties(priceRequest, price);
        return priceRepository.save(price).map(p2 -> buildPriceResponse(p2));
    });
...