I have a simple controller:
@RestController
public class SimpleController() {
public String get() {
if (System.nanoTime() % 2 == 0)
throw new IllegalArgumentException("oops");
return "ok"
}
}
Controller can throw simple exception, so i wrote controller advisor for it handling:
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public ResponseEntity<String> rejection(Rejection ex) {
return new ResponseEntity<>("bad", HttpStatus.CONFLICT);
}
Now i want to make get method async. But i don't know the best way for handling exception.
I tried:
public CompletableFuture<String> get() {
CompletableFuture.supplyAsync(
() -> {
if (System.nanoTime() % 2 == 0)
throw new IllegalArgumentException("oops");
return "ok";
}).exceptionally(thr -> {
//what should i do?
if (thr instanceof IllegalArgumentException)
throw ((IllegalArgumentException) t);
if (thr.getCause() instanceof IllegalArgumentException)
throw ((IllegalArgumentException) t.getCause());
return null;
}
}
But controller advisor still does not catch the exception.
Also i tried to return ResponseEntity("message", HttpStatuc.CONFLICT); in exceptionally block. But in tests i still have MvcResult.getResponse().getStatus() == 200.
Any other idea? Maybe it's a wrong way at all?
UPDATE I don't know why, but it don't catch exceptions:
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new AsyncUncaughtExceptionHandler() {
@Override
public void handleUncaughtException(Throwable ex, Method method, Object... params) {
System.out.println();
}
};
And even if it work, how to set http status to response?