0

I wrote the following code to upload a file:

@Slf4j
@Singleton
public class UploadService {

    @Inject
    @Client("${upload.url}")
    private RxHttpClient httpClient;

    public Flowable<HttpResponse<UploadFileResponse>> uploadFile(Path localFile) {

        MultipartBody requestBody = MultipartBody.builder()
            .addPart("file", localFile.getFileName().toString(), localFile.toFile())
            .build();

        return httpClient.exchange(
            HttpRequest.POST("/upload", requestBody).contentType(MULTIPART_FORM_DATA_TYPE),
            UploadFileResponse.class
        );
    }
}

And I'm trying to run it inside another map:

.....
return files
    .filter(file -> file.getLocalFile() != null)
    .parallel(transferParallelism)
    .runOn(Schedulers.io())
    .map(file ->
        file.withUploaded(
           uploadService.uploadFile(file.getLocalFile())
               .doOnError(throwable -> log.error("File " + file.getLocalFile().getFileName().toString() + " is not uploaded"))
               .map(it -> it.getBody(UploadFileResponse.class)
               .map(UploadFileResponse::isUploaded)
               .orElse(false))
               .blockingFirst()
        )
    )
    .sequential().toList()
    .map(....);
.......

If my storage is unavailable I have the following stacktrace:

io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.RuntimeException: java.lang.InterruptedException
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
    at io.reactivex.internal.operators.parallel.ParallelJoin$JoinSubscription.onError(ParallelJoin.java:191)
    at io.reactivex.internal.operators.parallel.ParallelJoin$JoinInnerSubscriber.onError(ParallelJoin.java:527)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.parallel.ParallelMap$ParallelMapSubscriber.onError(ParallelMap.java:131)
    at io.reactivex.internal.operators.parallel.ParallelMap$ParallelMapSubscriber.onNext(ParallelMap.java:117)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onNext(RxInstrumentedSubscriber.java:59)
    at io.reactivex.internal.operators.parallel.ParallelRunOn$RunOnSubscriber.run(ParallelRunOn.java:273)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.RuntimeException: java.lang.InterruptedException
    at io.reactivex.internal.util.ExceptionHelper.wrapOrThrow(ExceptionHelper.java:46)
    at io.reactivex.internal.subscribers.BlockingBaseSubscriber.blockingGet(BlockingBaseSubscriber.java:72)
    at io.reactivex.Flowable.blockingFirst(Flowable.java:5699)
    at com.wefi.commcache.FwCommCacheUpdater.lambda$updateCommCachePartitions$3(FwCommCacheUpdater.java:77)
    at io.reactivex.internal.operators.parallel.ParallelMap$ParallelMapSubscriber.onNext(ParallelMap.java:113)
... 9 more
Caused by: java.lang.InterruptedException
    at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1047)
    at java.base/java.util.concurrent.CountDownLatch.await(CountDownLatch.java:232)
    at io.reactivex.internal.subscribers.BlockingBaseSubscriber.blockingGet(BlockingBaseSubscriber.java:65)
... 12 more

Looks like .doOnError() is not working. I'm new in RxJava. Can anybody explain how to handle it? And am I calling my code correctly to get a boolean result?

Virkom
  • 373
  • 4
  • 22
  • You could handle this exception as try { //your code } catch(InterruptedException e) { System.out.println("got interrupted!"); } – Hasanuzzaman Rana Jul 23 '21 at 06:24
  • Thank you for your answer, but it's not allowed because my code calls inside another map. I added more details to my question. – Virkom Jul 23 '21 at 06:42
  • You might need to utilize observable, please check following URL as guide -https://stackoverflow.com/questions/33556076/how-do-i-handle-exceptions-in-map-in-an-observable-in-rxjava/42027961 – Hasanuzzaman Rana Jul 23 '21 at 06:49

0 Answers0