I am working on a microservice which does some calculation based on certain configurations stored in its own data store. The calculations apis are stored via REST APIs. The application is a spring boot application. Now there are mainly 3 layers in the application :
- REST Controller
- Service layer
- DAO layer - it used spring data.
I am planning to handle the logging and exception handling using below points:
Log each request that the service receives and response or at least the response if the status is not in 2xx series.
If there are any checked exception in either DAO layer or Service layer then log them and throw a custom exception derived from RuntimeException.
Have Several custom exception which should be thrown from Service layer mainly if we come across scenarios like invalid values, null values etc.
Have a try catch block in the REST Controller and log the exception i.e. message along with stacktrace and return the response accordingly.
So overall idea is to let the RuntimeExceptions propagate all the way to REST Controller where they should be logged and accordingly the response should be sent. Incase of checked exceptions log them in the same method and throw custom exceptions instead.
Please suggest what should be the correct or a good approach for logging exception in such applications.