Model Error Handling (MVC)

I was interested in that, except for the standard error handling in the Model.

Currently, I have setError and getError methods that are used by all my models.

This means that I only care if the method call in my model is true or false. If it is erroneous, I would use $this->model->getError() in my controller.

In addition, I am considering setting up a separate file that contains all my errors. One file per model also wanted to have thoughts on this.

+6
php model-view-controller error-handling model
source share
3 answers

A simpler solution would be to use exceptions .

If an error occurs that will be displayed by the user, enter a special type of exception - possibly with the name UserError . The exception must contain the text of the error message when it is thrown. These errors are functions that provide users with useful information (i.e., they tried to delete something that was not there, which could happen when they have several browsers open, etc.).

eg:.

 throw new UserError("That object no longer exists."); 

If an error occurs that you want to hide from the user, enter another type of exception, possibly called InternalError . You would like to log this and let the program continue to work, so the specific error is hidden from the user. If this prevents something, you may need to generate a generic error message. These will be errors, and you want to fix them as soon as possible.

eg:.

 throw new InternalError("Failed to connect to remote service"); 

All error messages can be stored (hard-coded) in the source where the exception is thrown. This is not necessarily a bad design β€” if you use a tool like gettext , you can easily translate all of these messages.

+3
source share

I use log4j and log4cxx and log in to syslogd. Kiwi is a simple Win32 system log that will track your log messages and save them to a file. Log4j / Log4cxx have configuration files that you can use to configure all levels of the log or the destination of the log messages (you can enter several places).

It takes so little effort to set up and use, and it works like a charm.

I have not tried log4php myself.

Exceptions are good when you no longer want your program to continue to run. Catch exceptions at a high level where you can accept rejection of unsuccessful executions.

+1
source share

Check out the NerdDinner tutorial on how to create validation procedures before making a final decision:

http://nerddinnerbook.s3.amazonaws.com/Part3.htm

Part of the validation is about 2/3 of the way down the page.

+1
source share

All Articles