I am trying out kotlin in a home project with Spring webflux and project reactor. I am trying to do a blocking call to the H2 database and I am therefore using the fromCallable method as recommended. To my understanding and experience, fromCallable is supposed to wrap any encountered exception which can then be handled using doOnError, but instead, the error is displayed directly in the console.
fun updateUser(req: ServerRequest): Mono<ServerResponse> =
req.bodyToMono(UserDto::class.java)
.flatMap { userDto -> updateUser(userDto) }
.flatMap { user -> ServerResponse.ok().syncBody(user!!) }
.doOnError { ServerResponse.notFound().build() }
fun updateUser(userDto: UserDto): Mono<User?> =
Mono.fromCallable {
val id = userDto.id.toLong()
userRepository.findByIdOrNull(id) ?:
throw IllegalArgumentException("No user found")
}.subscribeOn(Schedulers.elastic())
If I ask for an Id that does not exist in my database, I would expect a 404 back. Instead, I get a 500 back from the request and the IllegalArgumentException straight into my console in the IDE. If anyone can tell me why this is, or have any info about this, it would be greatly appreciated!
doOnError adds behavior if a mono terminates with an error. In other words, it adds a side effect but doesn't change the stream. Replace doOnError with onErrorResume. onErrorResume it exactly what you need, it subscribes to a fallback publisher if any error occurs.
fun updateUser(req: ServerRequest): Mono<ServerResponse> =
req.bodyToMono(UserDto::class.java)
.flatMap { userDto -> updateUser(userDto) }
.flatMap { user -> ServerResponse.ok().syncBody(user!!) }
.onErrorResume { ServerResponse.notFound().build() } // fallback publisher
.doOnError { println("Failed to perform an update: $it") } // side effect
Related
I am trying to make sure my app responds appropriate in the event of a backend failure, I am using realm/mongo to create an async task that fetches the user.
I have these two blocks:
override suspend fun logIn(accessToken: String) {
val user = logInInternal(accessToken)
realmAsyncOpen(user)
}
and
private suspend fun logInInternal(accessToken: String) = suspendCancellableCoroutine<User> { continuation ->
val customJWTCredentials: Credentials = Credentials.jwt(accessToken)
app.loginAsync(customJWTCredentials) {
if (it.isSuccess) {
continuation.resumeWith(Result.success(app.currentUser()!!))
} else {
continuation.resumeWithException(RealmLoginException().initCause(it.error))
}
}
}
logInInternal crashes when I hit the resumeWithException part. I have also tried using app.login(credentials) since the method is suspending, without luck there. Why does my app crash when I resume with exception?
I am causing the call to 502 out when hit.
The docs of resumeWithException say:
Resumes the execution of the corresponding coroutine so that the exception is re-thrown right after the last suspension point.
Which means you need to catch that exception:
override suspend fun logIn(accessToken: String) {
try {
val user = logInInternal(accessToken)
realmAsyncOpen(user)
} catch(e: RealmLoginException /*or e: Exception - to catch all exceptions*/) {
// handle exception
}
}
How to change the parameters with retry() in kotlin and webflux ?
There is a productInfo function, the function parameter is a collection of product ids.
When I input a wrong id in the list collection ids, the upstream interface will only return the wrong id. And get failed.
What I want to achieve is when the upstream interface returns the wrong id. The product info can remove the wrong id and have a second try with the right ids.
I tried to use retry() but I don't know how to change the parameters in the second try.
fun productInfo(ids: List<Pair<String, String>>): Flux<ProductItem> {
return productWebClient
.get()
.uri("product/items/${ids.joinToString(";") { "${it.second},${it.first}" }}")
.retrieve()
.bodyToFlux(ProductItem::class.java)
.onErrorResume {
logger.error("Fetch products failed." + it.message)
Mono.empty()
}
}
What you want is not retry(). I've built a solution making minor assumptions here and there. You can refer to this solution and make changes according to your requirements. I've used recursion here (productInfo()). You can replace the recursion call with webclient call if the error occurs only once.
fun productInfo(ids: List<Pair<String, String>>): Flux<ProductItem> {
val idsString = ids.joinToString(";") { "${it.second},${it.first}" }
return webClient
.get()
.uri("product/items/${idsString}")
.exchange()
.flatMapMany { response ->
if (response.statusCode().isError) {
response.body { clientHttpResponse, _ ->
clientHttpResponse.body.cast(String::class.java).collectList()
.flatMapMany<ProductItem> { eids ->
val ids2 = ids.filter { eids.contains("${it.second},${it.first}") }
productInfo(ids2)
}
}
} else {
response.bodyToFlux(ProductItem::class.java)
}
}
}
In the following code, I have a nested observable. The sendMessage in the flatMap calls the sendMessage function which is also an observable. If an exception occurs in this nested observable, the onExceptionResumeNext is suppose to catch the exception, process the exception and then continue on as though nothing happened. The exception does get caught but once the processing on the exception completes, no further emissions are made in the stream. Not even the doOnComplete is called. In essence, the onExceptionResume next just hangs.
I have tried onErrorReturnItem but have the same result. I have not found a single example in Stackoverflow or elsewhere for that matter that even shows onExceptionResumeNext or onErrorResumeNext or onErrorReturnItem inside a nested observable and after a day of working on it, I suspect that it may not be possible to support a nested error handler.
NOTE: In the onExceptionResumeNext I am currently just returning
Observable.empty<MessageToSend>()
In my actual code, I have code to process the exception and I tried returning an observable as well as just returning the data. Doesn't matter what I do - it always hangs.
fun postMessages() {
val msgToSendPublisher = BehaviorSubject.createDefault(MessageToSend())
msgToSendPublisher
.flatMap { _ ->
App.context.repository.getMessageToSend().toObservable()
}
.doOnError { error ->
if (error is EmptyResultSetException)
App.context.repository.setSendStatusToNotSendingForAllMessages()
}
.doOnNext { messageToSend ->
App.context.repository.updateMessage(messageToSend)
}
.flatMap { messageToSend ->
App.context.repository.sendMessage(messageToSend)
}
.doOnNext { messageToSend ->
messageToSend.dateSent = Date()
App.context.repository.updateDateLastMessageSent(messageToSend)
}
.doOnNext { messageToSend ->
if (messageToSend.totalMessagesToSend == 1)
App.context.repository.updateSendStatus(messageToSend, MessageSendStates.NOT_SENDING)
else
Observable.just(messageToSend)
}
.doOnNext {
msgToSendPublisher.onNext(it)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ messageToSend ->
},
{ ex ->
onMessagesSent()
},
{
onMessagesSent()
}
)
}
fun sendMessage(messageToSend: MessageToSend): Observable<MessageToSend> {
val obs = Observable.fromCallable {
if (messageToSend.totalMessagesToSend == 3)
throw Exception("Couldn't send to recipient.")
messageToSend
}.map {
storeMessageSent(messageToSend)
}.onExceptionResumeNext {
Observable.empty<MessageToSend>() // Hangs here.
).doOnComplete {
addNewMessageIfRequired(messageToSend, newMessage)
}
return obs
}
UPDATE:
I decided to test out a sample code I found that uses onExceptionResumeNext. It looks like this:
Observable.fromArray(1, 2, 3)
.doOnNext {
if (it == 2) {
throw (RuntimeException("Exception on 2"))
}
}
.onExceptionResumeNext(
Observable.just(10)
)
.subscribe(
{
var x = it
},
{
var x = it
},
{
var x = 0
x++
}
)
If you put a breakpoint on the line inside of the onExceptionResumeNext, it will get called every single time you run the observable for the first time and not just when the exception is thrown. This is clearly a behavior that is not identified in the RxJava documentation. Any developer will be under the impression that it will only get called when an exception is thrown. In the example above, setting the value to 10 is not really an issue. It's effectively just setting up the return value for the case when an exception occurs. However, if this was more elaborate code that stores stuff in the database (which my app does), it will get called when the observable is initialized - which is really bad. In spite of this discovery, it still does not solve my problem in that no further items are emitted. What I did discover in the sample code is that when onExceptionResumeNext is called, the onComplete is also called. Too bad the documentation doesn't mention that either.
You may want to use defer to defer execution of function calls that result in side-effects upon call:
Observable<Integer> createFallback() {
System.out.println("Why is this executing now?!");
return Observable.empty();
}
Observable.<Integer>error(new Exception())
.onExceptionResumeNext(createFallback())
.subscribe();
The createFallback runs because you specified it to run by invoking it. If the sequence is rewritten, it should become more apparent why:
Observable<Integer> fallback = createFallback();
Observable.<Integer>error(new Exception())
.onExceptionResumeNext(fallback)
.subscribe();
Now if you comment out the error-observable part, does it still execute createFallback()? Yes and RxJava is not even involved at that point yet.
If you want the side-effects to not happen to createFallback this way, you have to defer the execution of the entire method, there is an operator for that purpose: defer:
Observable.<Integer>error(new Exception())
.onExceptionResumeNext(Observable.defer(() -> createFallback()))
.subscribe();
I presume this looks something like this in Kotlin:
Observable.error(new Exception())
.onExceptionResumeNext(Observable.defer { createFallback() })
.subscribe()
during the last two days I read a lot about the rxJava retryWhen operator.
Here, here, here and some more I forgot.
But unfortunately I'm not able to get it work.
What I'm trying to achive is, that I make an API call. If the call returns an error, I'm showing a SnackBar to the user with a reload button. If the user clicks this button, I would like to resubscribe to the chain.
Here is my code:
interface RetrofitApi {
#GET("/v1/loadMyData")
fun getMyData(): Single<Response<DataResponse>>
}
Where Response is from retrofit2. I need it to wrap the data class to check if response is successful.
The next fun is called in a repository from a ViewModel:
override fun loadMyData(): Observable<Resource<DataResponse>> {
return retrofitApi
.getMyData()
.compose(getRetryTransformer())
.toObservable()
.compose(getResponseTransformer())
}
Resource is another wrapper for the state of the call (SUCCESS, ERROR, LOADING).
And finally the Transformer:
private fun <Data> getRetryTransformer(): SingleTransformer<Response<Data>, Response<Data>> {
return SingleTransformer { singleResponse ->
singleResponse
.onErrorReturn {
singleResponse.blockingGet()
}
.retryWhen { errors ->
errors.zipWith(retrySubject.toFlowable(BackpressureStrategy.LATEST),
BiFunction<Throwable, Boolean, Flowable<Throwable>> { throwable: Throwable, isRetryEnabled: Boolean ->
if (isRetryEnabled) {
Flowable.just(null)
} else {
Flowable.error(throwable)
}
})
}
}
}
The retrySubject:
private val retrySubject = PublishSubject.create<Boolean>()
And when the user clicks the retry button, I call:
retrySubject.onNext(true)
The problem is now, that the error is not returned to the ViewModel and the SnackBar is never shown. I tried onErrorResumeNext() as well with no success. The whole retryWhen/zipWith part seem to work. Because in the repository there some more API calls with no retry behavior (yet) and there the SnackBar is displayed. That means, I do anther call where the SnackBar is shown -> button click and the retry transform works as expected.
If you need some more information please don't hesitate to ask! Any help is appreciated!
Strange, as soon you do it the right way, it works.
I over read somehow that I need in doOnError{...} to manage to show my Snackbar.
Here is my working retry transformer:
private fun <Data> getRetryTransformer(): SingleTransformer<Response<Data>, Response<Data>> {
return SingleTransformer { singleResponse ->
singleResponse
.doOnError {
errorEventSubject.onNext(it)
}
.retryWhen { errors ->
errors.zipWith(retrySubject.toFlowable(BackpressureStrategy.LATEST),
BiFunction<Throwable, Boolean, Flowable<Throwable>> { throwable: Throwable, isRetryEnabled: Boolean ->
if (isRetryEnabled) {
Flowable.just(throwable)
} else {
Flowable.error(throwable)
}
})
}
}
}
And the chain looks now like this (and I think it's beautiful):
override fun loadMyData(): Observable<Resource<DataResponse>> {
return retrofitApi
.getMyData()
.compose(getRetryTransformer())
.toObservable()
.compose(getResponseTransformer())
}
What else I needed to propagate the error to my ViewModel is a 2nd PublishSubject:
private val errorEventSubject = PublishSubject.create<Throwable>()
And in the ViewModel I observe the changes for it and show the Snackbar.
That's it.
I am using io.vertx.reactivex.kafka.client.producer.KafkaProducer client. The client has a
rxWrite function which returns Single<RecordMetadata>. However I need to log error if any, during write operation. It apparently is not getting executed.
I have written following working example.
test(): Function to test the chaining and logging
fun test(): Single<Int> {
val data = Single.just(ArrayList<String>().apply {
add("Hello")
add("World")
})
data.flattenAsObservable<String> { list -> list }
.flatMap { advertiser ->
//does not work with writeKafka
writeError(advertiser).toObservable().doOnError({ println("Error $data") })
}
.subscribe({ record -> println(record) }, { e -> println("Error2 $e") })
return data.map { it.size }
}
writeKafka: Writes the given given string into Kafka and returns Single
fun writeKafka(param: String): Single<RecordMetadata> {
//null topic to produce IllegalArgumentException()
val record = KafkaProducerRecord.create(null, UUID.randomUUID().toString(), param)
return kafkaProducer.rxWrite(record)
}
writeError: Always return a single with error of same type
fun writeError(param: String): Single<RecordMetadata> {
return Single.error<RecordMetadata>(IllegalArgumentException())
}
So when I call writeKafka It only prints Error2 but if I use writeError it prints both Error and Error2. Looks like the single returned by writeKafka is still waiting for result, but then why even Error2 is printed?
I am pretty newbie in RxJava2, could somebody point out any error in that?
It is important to read and post the stacktrace of errors so that the problem can be isolated.
In this case, looks like you get the IllegalArgumentException from create and you don't get any Single because the relevant Kafka class throws it. return kafkaProducer.rxWrite(record) never executes at all and you practically crash the flatMap. doOnError never gets into play hence only the "Error2" is printed.