How to pass the error up the call tree classes?

Hello! There is a file input and output, let it be index.php
In it we define which controller to run and give the data back to the user using a General class of Response, which keeps all that you need to return:
  • Common errors
  • Input errors in the field
  • Data is ready to retrieve

Errors and data class are added using methods:
$Response->addErr("Message", 455214); // message and error code needed for front-end
$Response->addFieldErr("title", "Message"); // name of field with error and error message
$Response->addData("items", $posts_array); // json array of posts
$Response->addData("attachments", $attachments_array); // json array of attachments to posts

Well at the end index.php simply execute the function: $Response->returnResponse();

But here's the problem here.
For example, you need to add comment to post and the action code is the following (we sent the ID of the post and the comment text):
index.php causes the controller AddCommentInPost, the controller causes:
  • Model PostModeltrying to post to which we add a review in order to check whether there is such a post PostsModel->getPostById($_GET['id']);
  • Method model CommentsModel, which validates the incoming data and adds a comment to the database
  • If all is well, the model CommentsModel returns an object of the comment, CommentEntity

So... What if the validation error occurred? How do I get the fields with errors to the controller AddCommentInPost? I can't call a class Response from the model, because the model does not know anything about the outside world.

But with this mission, I have another idea.... for example, to CommentsModel in case of any error, gave falseand class AddCommentInPost, seeing that came is not an object but false knew that there was some error and is already using the method CommentsModel::getErrors() received and processed an array with the errors stored in a model.
We got this tree: index.php - AddCommentInPost - CommentsModel::getErrors()
And it turns out that all is well, because AddCommentInPost has a relationship with the Response and can write down the error. BUT WHAT IF between AddCommentInPost and CommentsModel was another class how to pass errors from CommentsModel in AddCommentInPost?
March 12th 20 at 08:43
2 answers
March 12th 20 at 08:45
To Use Exceptions
In the case if you throw Exception anywhere in the call tree it will just get to the top until you get caught in the catch block
In the place where a message is generated (in the controller) to catch and bring into the desired form
Thanks for the response! I read a lot of articles about exceptions, but everywhere it is recommended to use them only if the error is unexpected.
Perhaps there should be a more beautiful way. Or this is a beautiful way? It is important for me to understand, I tripped on some stones possible, which are not visible now - georgette.Lakin commented on March 12th 20 at 08:48
@georgette.Lakin, what's the error "foreseen"?
in fact, Yes, you can write code a little differently, without Exceptions in the controller, the controller only model, and a service, do some custom 'Validate', to do an array of 'errors', check it for length and so on.
But there are questions. What is the error? Returned from the function is false? null? an empty array? an array with the key 'errors'? an array with the key 'errors' where the value for key is not an empty array?

Exceptions is a basic design language, which certainly should not be neglected, and afraid to use it. An obvious mistake is always better than implicit
Prokinetic uncaught Exception will pop up another staging environment, while custom 'errors' can live undetected in the code on the prod over the years.
Specifically, normally the named Exception and even code readability adds

Purely for the sake of interest in the current project did a search on occurrences of Exceptions in the code base - 3038 occurrences (including the new throwing and catching, including in vendor). The project is of medium size, slightly less than 5 years

Again, the number of cases of acceptenv you can collect and analyze (

Articles on "the read" : - Melody_Krajc commented on March 12th 20 at 08:51
It perfectly describes why to use throw a good right and reliable choice - precious commented on March 12th 20 at 08:54
March 12th 20 at 08:47
to do explicit validation, not as a side effect.

$errors = $commentsModel->validate($comment);
if (!$errors) {
 $status = $commentsModel->save($comment);
In case if AddCommentInPost and CommentsModel was still one or more classes - the problem would have remained. - georgette.Lakin commented on March 12th 20 at 08:50
this methodology Schemes, modern PHP is better to generate iskluchenie - precious commented on March 12th 20 at 08:53

Find more questions by tags PHP