0

In my Spring MVC Based application, I have a ControllerAdvice defined.

Now, in one of my service classes, I have this parallelStream created:

rawImportList.parallelStream().forEach(rawImportBatch -> {
    rawImportBatch.parallelStream().forEach(this::importNominationFromRawImport)
});

The importNominationFromRawImport() method here, by definition, could throw a custom exception that should ideally be handled in the ControllerAdvice.

What could be the best way to achieve this?

Curious Coder
  • 646
  • 8
  • 18
  • The exception if thrown, and is defined in the ControllerAdvice should be caught there...I dont understand your question exactly :) What is happening now? What is it that you want to happen? – JCompetence Mar 10 '22 at 08:35
  • @SMA This line: `rawImportBatch.parallelStream().forEach(this::importNominationFromRawImport)` Complains that the exception needs to be handled. The only option I seem to be having here is to surround it with a try-catch block. That doesn't help since I cannot really propagate this to my API response. I hope this clarifies. – Curious Coder Mar 10 '22 at 08:43
  • 1
    Your case is the typical (Checked exception in a Lambda expression and how to handle it) A) Catch and convert to a CustomRunTimeException which you can catch in your ControllerAdvice B) Change this CheckedException you are throwing from `ImportNomination` to a CustomRunTimeException C) Look into jooq library which has `Unchecked.consumer()` which throws an UncheckedException – JCompetence Mar 10 '22 at 08:59
  • @SMA Option `A` worked for me, thanks! – Curious Coder Mar 16 '22 at 19:01

0 Answers0