When the ExceptionHandlerMethodResolver cannot handle the high-level exception, it tries only the first level of cause. In cases when there is a long cause chain, it will miss the most nested exceptions.

My real situation is: CompletionException -> feign.RetryableException -> UnknownHostException

The handler does not reach the root cause: UnknownHostException.

In my opinion, the correct approach would be iterating all the exceptions until the end is reached.

https://github.com/spring-projects/spring-framework/blob/5c1176786c9f0d02aa3db00ba9c5dba19a526ac2/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java#L134-L139

Comment From: sbrannen

In my opinion, the correct approach would be iterating all the exceptions until the end is reached.

The code in question (resolveMethodByThrowable(Throwable)) is a recursive function, which effectively iterates down to the root cause, halting on the first method found that can handle the current type of Throwable.

In light of that, I am closing this issue.

However, if you find that the recursive algorithm does not work, please provide an example demonstrating that, and we can reopen this issue to investigate further.

Comment From: dinhani

Sorry for the confusion, the implementation I tested locally was outdated and a bit different.

Indeed the issue existed, but it was solved by this fix: https://github.com/spring-projects/spring-framework/commit/b31f2bdad2d538c2573e1583a6a48edcd4d147e9