Hey guys, I have an implementation question about the service layer in a Java Spring application. We are using the repository design pattern.
In the past, I would have the service layer make calls to the repository, handle mapping the returned entities to DTOs, and then return the DTO or list of DTOs from the service layer and put them in the payload in the controller.
However, at my current job everybody wraps the response from service layers in a result class. This result class includes the possible data of a generic type, as well as some fields like "hasError", "errorMessages", and "statusCode".
My question is, is this best practice? Would it not make more sense to use something like @ControllerAdvice in Spring, and throw an exception with an associated status code?
I think they did this because they wanted the response to always be the same structure, whether or not there was an error. However, I think it might be better practice to just look at the status code that is returned, and make a decision based on that. "errorMessages" and the other fields could just be included in the exception class.
I am curious your guys thoughts on this. I have found many examples using springs error handling tools, but no examples yet that use this wrapper result pattern.