I'm developing a MVC Web application with a REST interface.  The REST controller performs actions on persisted items through a service class, which translates exceptions coming from the persistence layer.When a request involves an item that not exists in the database, I would like to return 404 code, but actually this is not possible due to the exception translation operated by the service, that prevents the controller from knowing the reason of the problem.Should the service class return a particular exception only for this case? Conversely, should the controller check for item existence before any action?
Should a client check for persisted item existence before modifying it?
java;mvc;rest;exceptions
The Service layer can translate exception received from lower layers to avoid coupling between the controller and the persistence layer. But in doing that translation, the Service layer should not throw away/obscure information that could be relevant for dealing with the reported error.So, the Service layer should in general have a one-to-one mapping between persistence exceptions and the exceptions they get translated into. You should only deviate from that if the persistence layer, for legacy reasons, throws two different exceptions for one and the same problem.