Throw new RuntimeException() from a Mono, in AOP

225 Views Asked by At

I have a reactive springboot application, in it an aspect

@Before("approveRejectPointcut()")
public void logAfterReturning(JoinPoint joinPoint) {
   
    Status newStatus = AspectUtils.returnFirstParameterOfTypeOrFail(allParameters, Status.class, "approveRejectPointcut");
    String comments = AspectUtils.returnFirstParameterOfTypeOrFail(allParameters, String.class, "approveRejectPointcut");
    
    Mono<Object> someObjectOrException = someService.updateApplicationStatus(appId, newStatus, comments);
    
    someObjectOrException
      .subscribe(i->log.info(i),
            error->{
                log.info("we have some ex: "+error.getMessage());
                throw new RuntimeException("PLEASE LORD: "+error.getMessage()); 
      });
}

What happens here, the updateApplicationStatus() from some service throws at some point a Business exception(which is good), in this aspect I print the exception message, which is also good. However, when i try to throw a business exception (a RuntimeException at it's base) .. i can see this exception in the console, however.. it does not reach the client. I suspect it is being thrown on a different thread. Any clues ?

1

There are 1 best solutions below

0
On

In the end, changed the aspect to @Around

@Around("approveRejectPointcut()")
public Mono<Object> logAfterReturning(ProceedingJoinPoint pjp) {
...
Mono<Object> someObjectOrException = someService.updateApplicationStatus(appId, newStatus, comments);
someObjectOrException
      .map(unused->pjp.proceed(pjp.getArgs())
      .onErrorMap(RuntimeException.class, (s)->{
            return new GenericBusinessException(s.getMessage());
      }); 
}

this way in case all goes well in the service -> we carry on with the execution, otherwise, we throw our business exception with the exception message from the service. Perhaps that map() should be replaced with some other operator accepting a funtion.. :)