I am working on an API using Laravel. I am writing business logic and application logic and trying my best to separate them as much as possible. However, I am new in such concepts. For example, I have the logic to log the user in. I have to check:
I am not checking all this in the controller(as I think it's not the controller's responsibility), but in a separate LoginFormProcessor
Class which is delegated by the Controller.
If all the checks passes, the LoginFormProcessor
will delegate the Object to save/get the object in/from the database. Here is the hierarchy:
Controller -> LoginFormProcessor -> Repository
I want to return the detailed JSON errors (if any) occurred in the LoginFormProcessor
or Repository
but not directly from these classes (as this is not their responsibility) but from the Controller.
How I can return the mentioned errors to Controller so the Controller could create a meaningful response and sent to the client.
Should I return some integer type errorCodes from the LoginFormProcessor
and Repository
? But then I'll have to check for all the possible error codes, an other headache. I don't think it's a good practice.
Any suggestion and good practices about it?
You want your business logic and your application logic to be agnostic of your delivery mechanism, which mean they should not return HttpCodes. Instead, you should use exceptions (custom exceptions when needed), catch them in your presentation layer (controllers) and transform them accordingly.
As you tagged your question with Laravel, you may have a look at https://laravel.com/docs/5.6/errors#render-method for how to catch the exceptions and translate them to HttpCodes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With