How to handle error on multiple chained Observable in RxJava?












0















I am developing an Android app using Kotlin, RxJava, and Retrofit.
I want to send Http Request to the server.




  1. PUT - update option of job

  2. POST - run the job


After the first request success, then I send a second request.
So I used concatMap.



val updateJob = restService.updateJob(token, job.id, options) // PUT
val runJob = restService.runJob(token, job.id) // POST

updateJob.concatMap { runJob }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success: $job")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})


What I want is to cancel, if the first request is failed.
How should I do this?



Here is a possible code.
But... this code is... I think this is ugly...
Is there any cool code, please?



disposable.add(
restService.updateJob(token, job.id, options) // PUT
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "updateJob - success")
restService.runJob(token, job.id) // POST
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})
}, {
Log.e(TAG, "updateJob - failed: ${it.message}")
it.printStackTrace()
})
)




I have one more question.
I have the job list.
And I want to do the same thing.
Even if some jobs fail, I want to continue with the next jobs.
I considered "onErrorResumeNext, onErrorReturn, doOnError".
But they are not the solution.
How can I do this?



Observable.fromIterable(jobs)
.concatMap { job ->
val updateJob = restService.updateJob(token, job.id, options)
val printJob = restService.printJob(token, job.id)

updateJob.concatMap { printJob }
}
.window(1) // I thought "window" can be the solution. But it doesn't work.
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJobs - success")

}, {
Log.e(TAG, "runJobs - failed: ${it.message}")
it.printStackTrace()
})









share|improve this question

























  • please check the answer for your second question. And please put the new question as another StackOverflow question next time.

    – Prithvi Bhola
    Nov 22 '18 at 11:57
















0















I am developing an Android app using Kotlin, RxJava, and Retrofit.
I want to send Http Request to the server.




  1. PUT - update option of job

  2. POST - run the job


After the first request success, then I send a second request.
So I used concatMap.



val updateJob = restService.updateJob(token, job.id, options) // PUT
val runJob = restService.runJob(token, job.id) // POST

updateJob.concatMap { runJob }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success: $job")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})


What I want is to cancel, if the first request is failed.
How should I do this?



Here is a possible code.
But... this code is... I think this is ugly...
Is there any cool code, please?



disposable.add(
restService.updateJob(token, job.id, options) // PUT
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "updateJob - success")
restService.runJob(token, job.id) // POST
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})
}, {
Log.e(TAG, "updateJob - failed: ${it.message}")
it.printStackTrace()
})
)




I have one more question.
I have the job list.
And I want to do the same thing.
Even if some jobs fail, I want to continue with the next jobs.
I considered "onErrorResumeNext, onErrorReturn, doOnError".
But they are not the solution.
How can I do this?



Observable.fromIterable(jobs)
.concatMap { job ->
val updateJob = restService.updateJob(token, job.id, options)
val printJob = restService.printJob(token, job.id)

updateJob.concatMap { printJob }
}
.window(1) // I thought "window" can be the solution. But it doesn't work.
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJobs - success")

}, {
Log.e(TAG, "runJobs - failed: ${it.message}")
it.printStackTrace()
})









share|improve this question

























  • please check the answer for your second question. And please put the new question as another StackOverflow question next time.

    – Prithvi Bhola
    Nov 22 '18 at 11:57














0












0








0








I am developing an Android app using Kotlin, RxJava, and Retrofit.
I want to send Http Request to the server.




  1. PUT - update option of job

  2. POST - run the job


After the first request success, then I send a second request.
So I used concatMap.



val updateJob = restService.updateJob(token, job.id, options) // PUT
val runJob = restService.runJob(token, job.id) // POST

updateJob.concatMap { runJob }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success: $job")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})


What I want is to cancel, if the first request is failed.
How should I do this?



Here is a possible code.
But... this code is... I think this is ugly...
Is there any cool code, please?



disposable.add(
restService.updateJob(token, job.id, options) // PUT
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "updateJob - success")
restService.runJob(token, job.id) // POST
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})
}, {
Log.e(TAG, "updateJob - failed: ${it.message}")
it.printStackTrace()
})
)




I have one more question.
I have the job list.
And I want to do the same thing.
Even if some jobs fail, I want to continue with the next jobs.
I considered "onErrorResumeNext, onErrorReturn, doOnError".
But they are not the solution.
How can I do this?



Observable.fromIterable(jobs)
.concatMap { job ->
val updateJob = restService.updateJob(token, job.id, options)
val printJob = restService.printJob(token, job.id)

updateJob.concatMap { printJob }
}
.window(1) // I thought "window" can be the solution. But it doesn't work.
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJobs - success")

}, {
Log.e(TAG, "runJobs - failed: ${it.message}")
it.printStackTrace()
})









share|improve this question
















I am developing an Android app using Kotlin, RxJava, and Retrofit.
I want to send Http Request to the server.




  1. PUT - update option of job

  2. POST - run the job


After the first request success, then I send a second request.
So I used concatMap.



val updateJob = restService.updateJob(token, job.id, options) // PUT
val runJob = restService.runJob(token, job.id) // POST

updateJob.concatMap { runJob }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success: $job")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})


What I want is to cancel, if the first request is failed.
How should I do this?



Here is a possible code.
But... this code is... I think this is ugly...
Is there any cool code, please?



disposable.add(
restService.updateJob(token, job.id, options) // PUT
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "updateJob - success")
restService.runJob(token, job.id) // POST
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})
}, {
Log.e(TAG, "updateJob - failed: ${it.message}")
it.printStackTrace()
})
)




I have one more question.
I have the job list.
And I want to do the same thing.
Even if some jobs fail, I want to continue with the next jobs.
I considered "onErrorResumeNext, onErrorReturn, doOnError".
But they are not the solution.
How can I do this?



Observable.fromIterable(jobs)
.concatMap { job ->
val updateJob = restService.updateJob(token, job.id, options)
val printJob = restService.printJob(token, job.id)

updateJob.concatMap { printJob }
}
.window(1) // I thought "window" can be the solution. But it doesn't work.
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJobs - success")

}, {
Log.e(TAG, "runJobs - failed: ${it.message}")
it.printStackTrace()
})






android rx-java reactive-programming rx-android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:15







yoonhok

















asked Nov 22 '18 at 6:54









yoonhokyoonhok

1981314




1981314













  • please check the answer for your second question. And please put the new question as another StackOverflow question next time.

    – Prithvi Bhola
    Nov 22 '18 at 11:57



















  • please check the answer for your second question. And please put the new question as another StackOverflow question next time.

    – Prithvi Bhola
    Nov 22 '18 at 11:57

















please check the answer for your second question. And please put the new question as another StackOverflow question next time.

– Prithvi Bhola
Nov 22 '18 at 11:57





please check the answer for your second question. And please put the new question as another StackOverflow question next time.

– Prithvi Bhola
Nov 22 '18 at 11:57












1 Answer
1






active

oldest

votes


















1














Actually, you have already given the answer.
Your first case is correct



val updateJob = restService.updateJob(token, job.id, options) // PUT
val runJob = restService.runJob(token, job.id) // POST

updateJob.concatMap { runJob }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success: $job")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})


So in this case, if updateJob request gets failed then the stream will move into error stream and runJob request will never be called.



runJob will be called only when updateJob is successful.



And after updateJob success if runJob fails, then also error stream will be called.



There is no need for your second solution.



And for your second question onErrorResumeNext should work. Return any dummy value and handle it in the onNext



Observable.fromIterable(jobs)
.concatMap { job -> restService.updateJob(token, job.id, options).onErrorResumeNext(Flowable.just(/*Send any dummy value*/)) }
.contcatMap { job -> restService.printJob(token, job.id).onErrorResumeNext{Flowable.just(/*Send any dummy value*/)} }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
/*If dummy value received....handle it gracefully*/
Log.d(TAG, "runJobs - success")

}, {
Log.e(TAG, "runJobs - failed: ${it.message}")
it.printStackTrace()
})





share|improve this answer


























  • Oh... Thank you :) I am the newbie.. It's not easy for me.

    – yoonhok
    Nov 22 '18 at 8:00











  • I am glad it helped :)

    – Prithvi Bhola
    Nov 22 '18 at 8:06











  • I added a question, could you help me again, please?

    – yoonhok
    Nov 22 '18 at 9:59











  • @yoonhok please check the answer

    – Prithvi Bhola
    Nov 22 '18 at 11:57











  • Hmm... it is not the solution. it is not what I want. I make a question newly for the second question, could you check it again, please? stackoverflow.com/questions/53439986/…

    – yoonhok
    Nov 23 '18 at 2:31













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425366%2fhow-to-handle-error-on-multiple-chained-observable-in-rxjava%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Actually, you have already given the answer.
Your first case is correct



val updateJob = restService.updateJob(token, job.id, options) // PUT
val runJob = restService.runJob(token, job.id) // POST

updateJob.concatMap { runJob }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success: $job")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})


So in this case, if updateJob request gets failed then the stream will move into error stream and runJob request will never be called.



runJob will be called only when updateJob is successful.



And after updateJob success if runJob fails, then also error stream will be called.



There is no need for your second solution.



And for your second question onErrorResumeNext should work. Return any dummy value and handle it in the onNext



Observable.fromIterable(jobs)
.concatMap { job -> restService.updateJob(token, job.id, options).onErrorResumeNext(Flowable.just(/*Send any dummy value*/)) }
.contcatMap { job -> restService.printJob(token, job.id).onErrorResumeNext{Flowable.just(/*Send any dummy value*/)} }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
/*If dummy value received....handle it gracefully*/
Log.d(TAG, "runJobs - success")

}, {
Log.e(TAG, "runJobs - failed: ${it.message}")
it.printStackTrace()
})





share|improve this answer


























  • Oh... Thank you :) I am the newbie.. It's not easy for me.

    – yoonhok
    Nov 22 '18 at 8:00











  • I am glad it helped :)

    – Prithvi Bhola
    Nov 22 '18 at 8:06











  • I added a question, could you help me again, please?

    – yoonhok
    Nov 22 '18 at 9:59











  • @yoonhok please check the answer

    – Prithvi Bhola
    Nov 22 '18 at 11:57











  • Hmm... it is not the solution. it is not what I want. I make a question newly for the second question, could you check it again, please? stackoverflow.com/questions/53439986/…

    – yoonhok
    Nov 23 '18 at 2:31


















1














Actually, you have already given the answer.
Your first case is correct



val updateJob = restService.updateJob(token, job.id, options) // PUT
val runJob = restService.runJob(token, job.id) // POST

updateJob.concatMap { runJob }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success: $job")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})


So in this case, if updateJob request gets failed then the stream will move into error stream and runJob request will never be called.



runJob will be called only when updateJob is successful.



And after updateJob success if runJob fails, then also error stream will be called.



There is no need for your second solution.



And for your second question onErrorResumeNext should work. Return any dummy value and handle it in the onNext



Observable.fromIterable(jobs)
.concatMap { job -> restService.updateJob(token, job.id, options).onErrorResumeNext(Flowable.just(/*Send any dummy value*/)) }
.contcatMap { job -> restService.printJob(token, job.id).onErrorResumeNext{Flowable.just(/*Send any dummy value*/)} }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
/*If dummy value received....handle it gracefully*/
Log.d(TAG, "runJobs - success")

}, {
Log.e(TAG, "runJobs - failed: ${it.message}")
it.printStackTrace()
})





share|improve this answer


























  • Oh... Thank you :) I am the newbie.. It's not easy for me.

    – yoonhok
    Nov 22 '18 at 8:00











  • I am glad it helped :)

    – Prithvi Bhola
    Nov 22 '18 at 8:06











  • I added a question, could you help me again, please?

    – yoonhok
    Nov 22 '18 at 9:59











  • @yoonhok please check the answer

    – Prithvi Bhola
    Nov 22 '18 at 11:57











  • Hmm... it is not the solution. it is not what I want. I make a question newly for the second question, could you check it again, please? stackoverflow.com/questions/53439986/…

    – yoonhok
    Nov 23 '18 at 2:31
















1












1








1







Actually, you have already given the answer.
Your first case is correct



val updateJob = restService.updateJob(token, job.id, options) // PUT
val runJob = restService.runJob(token, job.id) // POST

updateJob.concatMap { runJob }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success: $job")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})


So in this case, if updateJob request gets failed then the stream will move into error stream and runJob request will never be called.



runJob will be called only when updateJob is successful.



And after updateJob success if runJob fails, then also error stream will be called.



There is no need for your second solution.



And for your second question onErrorResumeNext should work. Return any dummy value and handle it in the onNext



Observable.fromIterable(jobs)
.concatMap { job -> restService.updateJob(token, job.id, options).onErrorResumeNext(Flowable.just(/*Send any dummy value*/)) }
.contcatMap { job -> restService.printJob(token, job.id).onErrorResumeNext{Flowable.just(/*Send any dummy value*/)} }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
/*If dummy value received....handle it gracefully*/
Log.d(TAG, "runJobs - success")

}, {
Log.e(TAG, "runJobs - failed: ${it.message}")
it.printStackTrace()
})





share|improve this answer















Actually, you have already given the answer.
Your first case is correct



val updateJob = restService.updateJob(token, job.id, options) // PUT
val runJob = restService.runJob(token, job.id) // POST

updateJob.concatMap { runJob }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
Log.d(TAG, "runJob - success: $job")
}, {
Log.e(TAG, "runJob - failed: ${it.message}")
it.printStackTrace()
})


So in this case, if updateJob request gets failed then the stream will move into error stream and runJob request will never be called.



runJob will be called only when updateJob is successful.



And after updateJob success if runJob fails, then also error stream will be called.



There is no need for your second solution.



And for your second question onErrorResumeNext should work. Return any dummy value and handle it in the onNext



Observable.fromIterable(jobs)
.concatMap { job -> restService.updateJob(token, job.id, options).onErrorResumeNext(Flowable.just(/*Send any dummy value*/)) }
.contcatMap { job -> restService.printJob(token, job.id).onErrorResumeNext{Flowable.just(/*Send any dummy value*/)} }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ job ->
/*If dummy value received....handle it gracefully*/
Log.d(TAG, "runJobs - success")

}, {
Log.e(TAG, "runJobs - failed: ${it.message}")
it.printStackTrace()
})






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 11:54

























answered Nov 22 '18 at 7:07









Prithvi BholaPrithvi Bhola

1,033319




1,033319













  • Oh... Thank you :) I am the newbie.. It's not easy for me.

    – yoonhok
    Nov 22 '18 at 8:00











  • I am glad it helped :)

    – Prithvi Bhola
    Nov 22 '18 at 8:06











  • I added a question, could you help me again, please?

    – yoonhok
    Nov 22 '18 at 9:59











  • @yoonhok please check the answer

    – Prithvi Bhola
    Nov 22 '18 at 11:57











  • Hmm... it is not the solution. it is not what I want. I make a question newly for the second question, could you check it again, please? stackoverflow.com/questions/53439986/…

    – yoonhok
    Nov 23 '18 at 2:31





















  • Oh... Thank you :) I am the newbie.. It's not easy for me.

    – yoonhok
    Nov 22 '18 at 8:00











  • I am glad it helped :)

    – Prithvi Bhola
    Nov 22 '18 at 8:06











  • I added a question, could you help me again, please?

    – yoonhok
    Nov 22 '18 at 9:59











  • @yoonhok please check the answer

    – Prithvi Bhola
    Nov 22 '18 at 11:57











  • Hmm... it is not the solution. it is not what I want. I make a question newly for the second question, could you check it again, please? stackoverflow.com/questions/53439986/…

    – yoonhok
    Nov 23 '18 at 2:31



















Oh... Thank you :) I am the newbie.. It's not easy for me.

– yoonhok
Nov 22 '18 at 8:00





Oh... Thank you :) I am the newbie.. It's not easy for me.

– yoonhok
Nov 22 '18 at 8:00













I am glad it helped :)

– Prithvi Bhola
Nov 22 '18 at 8:06





I am glad it helped :)

– Prithvi Bhola
Nov 22 '18 at 8:06













I added a question, could you help me again, please?

– yoonhok
Nov 22 '18 at 9:59





I added a question, could you help me again, please?

– yoonhok
Nov 22 '18 at 9:59













@yoonhok please check the answer

– Prithvi Bhola
Nov 22 '18 at 11:57





@yoonhok please check the answer

– Prithvi Bhola
Nov 22 '18 at 11:57













Hmm... it is not the solution. it is not what I want. I make a question newly for the second question, could you check it again, please? stackoverflow.com/questions/53439986/…

– yoonhok
Nov 23 '18 at 2:31







Hmm... it is not the solution. it is not what I want. I make a question newly for the second question, could you check it again, please? stackoverflow.com/questions/53439986/…

– yoonhok
Nov 23 '18 at 2:31




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425366%2fhow-to-handle-error-on-multiple-chained-observable-in-rxjava%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'