Error Handling in Reactive Flows
In some workflows, it is essential that interrupted sequences are flagged proactively as errors rather than simply returning empty Mono or Flux streams.
This enables upstream callers to register errors and to know that a flow was not complete. In workflow scenarios, use cases that cause premature termination of the stream should explicitly return an error object and not just a .empty()
Example handling from BorrowingAgencyService:
Code Block |
---|
return Mono.from(clusterRecordRepository.findById(bibClusterId)) .flatMap(clusterRecord -> Mono.from(bibRepository.findById(clusterRecord.getSelectedBib()))) .map(this::extractBibData) .flatMap(hostLmsClient::createBib) .map(patronRequest::setLocalBibId) .switchIfEmpty(Mono.error(new RuntimeException("Failed to create virtual bib."))) .map(pr -> Tuples.of(pr, patronIdentity, hostLmsClient, supplierRequest)); |