0

I have an API with 2 layers Controller and Service with Micronaut, Kotlin & Project Reactor. This application needs to handle requests in non-blocking manner. I have a Post function makePayment method as below

fun makePayment(payment: Payment): Mono<PaymentDetails> in class PaymentController.

This calls a service class PaymentService which also has the same function as above. This service internally uses io.micronaut.reactor.http.client.ReactorHttpClient to make the payment with Bank.

What is the right way to handle exception here?

Option1

reactorHttpClient.exchange().onErrorResume()

In onErrorResume I will map it to custom error code with suitable HTTP Status.No other specific Global Error handling is done.

Option2

Uses Controller Advise approach to have a Global Exception Handler with @Error(global = true) as mentioned below. https://docs.micronaut.io/latest/guide/index.html#globalErrorHandling

I tried both approaches and both works. I would like to understand what is the right approach considering Non-Blocking server and reactive streams approach. Also does the Global Exception Handler with @Error(global = true) going to work like a Blocking or Non-Blocking way?

2
  • Option #3: Custom implementation of ErrorResponseProcessor and a custom error DTO. BTW, you may simplify your code by using a declarative client, they too can return reactive types. BR
    – Roar S.
    Jul 16, 2022 at 11:08
  • Thank You for the input. I mostly see the reactive onError used during project reactor code as that's how reactive streams handle errors. In micronaut context of handling error using @Error , is it a blocking approach? or still can I use that in a reactive context?
    – vinilpj
    Jul 18, 2022 at 3:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.