This may seem like an odd question but it's something I've yet to find a "proper" answer for. I've tried googling but I don't get anything useful (maybe I'm looking for the wrong terms).
I'm attending a couple of classes where I'm building Web APIs, one using Spring Boot and the other using NodeJS (with Express), and we've been told to use logical layers like the "Service Layer" or the "Data Layer" but I haven't yet fully understood which responsibilities should belong to whom.
For instance, in the Spring project, I get a POST request and my handler receives a DTO and then I need to perform these steps:
- Transform the DTO into a Model object
- Validate the model according to the business rules
- Throw an exception if validaton fails and return an error response
- Save the model to the database
- Return an OK response
I'm having a hard time understanding to which "logical" layers each step belongs to (since I don't fully understand the layering yet), specially who handles the errors and how. Like, if validation fails due to business rules the exception I throw shouldn't know about HTTP but then whose job is it to catch it and map into a proper HTTP error?
Thanks for the help.