Search This Blog


Error control

Error control is a strategy used to catch situations that blocks a sucessful result of an application operation. This can be viewed in diferent levels of an application. At low level: one function could receive a unespected parameter. At high level, a tool could not accept a specific set of actions from the user.

I think it is fundamental distinguish what is a low level error from a high level one. Each one can get a diferent course and can be also shown to the user by a diferent way. If, by any reason, the programmer has created a SQL with syntax error and, because this, the user could not update a value in the DB, it is not correct to show that the problem was found at the SQL generation. After all, the final user might not know (and should not know) what is SQL. However, if the final user has filled a form field with an invalid value, we should show details about what they gone wrong and/or how they can fix it.

Using native exceptions from PHP language is a solution specially indicated to treat low level errors. At high level errors, we could also use it, but I prefer using validation methods to treat user input, then I put user errors in an array. Note that, there is no attempt to adjust user input, so the user should adjust by yourself. At other hand, a low level error can be bypassed (or not).

The behavior of exceptions can be simple, but it can offer sophisticated solutions. I do not want to discuss about exceptions syntax and use. So, if you want to know more about it, you can read the PHP manual: If you do not like to create many exceptions, maybe you could know the native exceptions from SPL:

A moderate solution to error control is creating methods that returns boolean values (true = was ok / false = something wrong) and, optionally, the method could receive an array parameter by reference. So, if some problem occours, the error array is filled by the message and the method returns false.

For the errors that never should occour (for example, an error by the programmer that did not used a language feature correctly), it is possible to use exception, but there is an other feature to that: the error log trigger. It can be done by the function trigger_error. The funciton receives an error message and an error level. The error levels that can be triggered by the programmer are:

  • E_USER_NOTICE - When you want to show a notice to programmer (it is not exactly an error). For example: a function has used more memory that was expected.
  • E_USER_WARNING - When you want to show a warning to programmer (a error, but not so serious). For example: the connection with DB fails (because an unknown reason), but the page still can be generated with a message to the client.
  • E_USER_ERROR - When the error was fatal and the script must be stoped immediately. For example, one method should receive obligatorily a parameter from a specific data type, but the programmer has used other type.
  • E_USER_DEPRECATED - A special kink of warning that indicates that the method is deprecated, and should not be used anymore, or it was replaced by an other feature to take the same result.

There are two PHP function specially useful to standardize the process flow of the errors and exceptions did not catch by try/catch. They are: set_error_handler and set_exception_handler. With they, it is possible, for example, keep some kind of errors in database to make searches easily, control the amount of errors in a period, etc.

No comments:

Post a Comment