I am by no means an expert in this particular field, but since no one else has weighed, I will give my two cents.
The solution you're connected with seems like a reasonable approach to me. Given the little cooperation between your handlers and the middleware exceptions, you can also flag your exceptions with additional information that may be useful in visualizing the error response, without actual error handling information crawling into your application logic.
So, to your first question: perhaps you could adapt your system to a specific specific use case, but as a general-purpose error handling scheme, this seems pretty good. There is nothing that pops up on me as a direct "wrong."
To your second question: itβs bad practice to catch the general type of Exception when you know that you are looking for a more specific one because you want to avoid combining expected and unexpected errors. If you know that there is a MissingResourceException possibility when you search for packages, you would not want your exception handler to accidentally remove NullPointerException bubbles from the actual error in your code.
In this case, however, I would say that looking for a typical type of Exception is exactly what you need to do. Instead of handling certain conditions, such as MissingResourceException , the goal of this top-level handler is to capture everything that your application logic does not and convert it to error information that matters to your API client. This is kind of the last line of defense between your implementation and its consumer at the other end.
source share